Search in sources :

Example 6 with ConvertibleValues

use of io.micronaut.core.convert.value.ConvertibleValues 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

ConvertibleValues (io.micronaut.core.convert.value.ConvertibleValues)6 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)3 Argument (io.micronaut.core.type.Argument)2 CollectionUtils (io.micronaut.core.util.CollectionUtils)2 URI (java.net.URI)2 Optional (java.util.Optional)2 Function (java.util.function.Function)2 BeanLocator (io.micronaut.context.BeanLocator)1 Parameter (io.micronaut.context.annotation.Parameter)1 Prototype (io.micronaut.context.annotation.Prototype)1 Environment (io.micronaut.context.env.Environment)1 AnnotationClassValue (io.micronaut.core.annotation.AnnotationClassValue)1 Internal (io.micronaut.core.annotation.Internal)1 AbstractAnnotatedArgumentBinder (io.micronaut.core.bind.annotation.AbstractAnnotatedArgumentBinder)1 ArgumentConversionContext (io.micronaut.core.convert.ArgumentConversionContext)1 ConversionService (io.micronaut.core.convert.ConversionService)1 MultiValuesConverterFactory (io.micronaut.core.convert.converters.MultiValuesConverterFactory)1 ConversionErrorException (io.micronaut.core.convert.exceptions.ConversionErrorException)1 Format (io.micronaut.core.convert.format.Format)1 FormattingTypeConverter (io.micronaut.core.convert.format.FormattingTypeConverter)1