use of io.micronaut.http.uri.UriMatchInfo in project micronaut-core by micronaut-projects.
the class PathVariableAnnotationBinder method bind.
@Override
public BindingResult<T> bind(ArgumentConversionContext<T> context, HttpRequest<?> source) {
ConvertibleMultiValues<String> parameters = source.getParameters();
Argument<T> argument = context.getArgument();
AnnotationMetadata annotationMetadata = argument.getAnnotationMetadata();
boolean hasAnnotation = annotationMetadata.hasAnnotation(PathVariable.class);
String parameterName = annotationMetadata.stringValue(PathVariable.class).orElse(argument.getName());
// If we need to bind all request params to command object
// checks if the variable is defined with modifier char *
// eg. ?pojo*
final Optional<UriMatchInfo> matchInfo = source.getAttribute(HttpAttributes.ROUTE_MATCH, UriMatchInfo.class);
boolean bindAll = matchInfo.flatMap(umi -> umi.getVariables().stream().filter(v -> v.getName().equals(parameterName)).findFirst().map(UriMatchVariable::isExploded)).orElse(false);
BindingResult<T> result;
// be manipulated to override POST or JSON variables
if (hasAnnotation && matchInfo.isPresent()) {
final ConvertibleValues<Object> variableValues = ConvertibleValues.of(matchInfo.get().getVariableValues());
if (bindAll) {
Object value;
// Only maps and POJOs will "bindAll", lists work like normal
if (Iterable.class.isAssignableFrom(argument.getType())) {
value = doResolve(context, variableValues, parameterName);
if (value == null) {
value = Collections.emptyList();
}
} else {
value = parameters.asMap();
}
result = doConvert(value, context);
} else {
result = doBind(context, variableValues, parameterName);
}
} else {
// noinspection unchecked
result = BindingResult.EMPTY;
}
return result;
}
Aggregations