Search in sources :

Example 1 with UriMatchInfo

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;
}
Also used : ConvertibleMultiValues(io.micronaut.core.convert.value.ConvertibleMultiValues) ConvertibleValues(io.micronaut.core.convert.value.ConvertibleValues) UriMatchVariable(io.micronaut.http.uri.UriMatchVariable) ArgumentConversionContext(io.micronaut.core.convert.ArgumentConversionContext) AbstractAnnotatedArgumentBinder(io.micronaut.core.bind.annotation.AbstractAnnotatedArgumentBinder) PathVariable(io.micronaut.http.annotation.PathVariable) UriMatchInfo(io.micronaut.http.uri.UriMatchInfo) Optional(java.util.Optional) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) HttpAttributes(io.micronaut.http.HttpAttributes) Argument(io.micronaut.core.type.Argument) HttpRequest(io.micronaut.http.HttpRequest) ConversionService(io.micronaut.core.convert.ConversionService) Collections(java.util.Collections) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) UriMatchVariable(io.micronaut.http.uri.UriMatchVariable) UriMatchInfo(io.micronaut.http.uri.UriMatchInfo) PathVariable(io.micronaut.http.annotation.PathVariable)

Aggregations

AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)1 AbstractAnnotatedArgumentBinder (io.micronaut.core.bind.annotation.AbstractAnnotatedArgumentBinder)1 ArgumentConversionContext (io.micronaut.core.convert.ArgumentConversionContext)1 ConversionService (io.micronaut.core.convert.ConversionService)1 ConvertibleMultiValues (io.micronaut.core.convert.value.ConvertibleMultiValues)1 ConvertibleValues (io.micronaut.core.convert.value.ConvertibleValues)1 Argument (io.micronaut.core.type.Argument)1 HttpAttributes (io.micronaut.http.HttpAttributes)1 HttpRequest (io.micronaut.http.HttpRequest)1 PathVariable (io.micronaut.http.annotation.PathVariable)1 UriMatchInfo (io.micronaut.http.uri.UriMatchInfo)1 UriMatchVariable (io.micronaut.http.uri.UriMatchVariable)1 Collections (java.util.Collections)1 Optional (java.util.Optional)1