Search in sources :

Example 1 with UnresolvedArgument

use of io.micronaut.web.router.UnresolvedArgument in project micronaut-core by micronaut-projects.

the class RequestArgumentSatisfier method getValueForArgument.

/**
 * @param argument         The argument
 * @param request          The HTTP request
 * @param satisfyOptionals Whether to satisfy optionals
 * @return An {@link Optional} for the value
 */
protected Optional<Object> getValueForArgument(Argument argument, HttpRequest<?> request, boolean satisfyOptionals) {
    Object value = null;
    Optional<ArgumentBinder> registeredBinder = binderRegistry.findArgumentBinder(argument, request);
    if (registeredBinder.isPresent()) {
        ArgumentBinder argumentBinder = registeredBinder.get();
        ArgumentConversionContext conversionContext = ConversionContext.of(argument, request.getLocale().orElse(null), request.getCharacterEncoding());
        if (argumentBinder instanceof BodyArgumentBinder) {
            if (argumentBinder instanceof NonBlockingBodyArgumentBinder) {
                ArgumentBinder.BindingResult bindingResult = argumentBinder.bind(conversionContext, request);
                if (bindingResult.isPresentAndSatisfied()) {
                    value = bindingResult.get();
                } else if (bindingResult.isSatisfied() && argument.isNullable()) {
                    value = NullArgument.INSTANCE;
                }
            } else {
                value = getValueForBlockingBodyArgumentBinder(request, argumentBinder, conversionContext);
            }
        } else if (argumentBinder instanceof RequestBeanAnnotationBinder) {
            // Resolve RequestBean after filters since some field types may depend on filters, i.e. Authentication
            value = (UnresolvedArgument<?>) () -> argumentBinder.bind(conversionContext, request);
        } else {
            ArgumentBinder.BindingResult bindingResult = argumentBinder.bind(conversionContext, request);
            if (argument.getType() == Optional.class) {
                if (bindingResult.isSatisfied() || satisfyOptionals) {
                    Optional optionalValue = bindingResult.getValue();
                    if (optionalValue.isPresent()) {
                        value = optionalValue.get();
                    } else {
                        value = optionalValue;
                    }
                }
            } else if (bindingResult.isPresentAndSatisfied()) {
                value = bindingResult.get();
            } else if (bindingResult.isSatisfied() && argument.isNullable()) {
                value = NullArgument.INSTANCE;
            } else if (HttpMethod.requiresRequestBody(request.getMethod()) || argument.isNullable() || conversionContext.hasErrors()) {
                value = (UnresolvedArgument) () -> {
                    ArgumentBinder.BindingResult result = argumentBinder.bind(conversionContext, request);
                    Optional<ConversionError> lastError = conversionContext.getLastError();
                    if (lastError.isPresent()) {
                        return (ArgumentBinder.BindingResult) () -> lastError;
                    }
                    return result;
                };
            }
        }
    }
    return Optional.ofNullable(value);
}
Also used : NonBlockingBodyArgumentBinder(io.micronaut.http.bind.binders.NonBlockingBodyArgumentBinder) UnresolvedArgument(io.micronaut.web.router.UnresolvedArgument) ArgumentConversionContext(io.micronaut.core.convert.ArgumentConversionContext) Optional(java.util.Optional) BodyArgumentBinder(io.micronaut.http.bind.binders.BodyArgumentBinder) NonBlockingBodyArgumentBinder(io.micronaut.http.bind.binders.NonBlockingBodyArgumentBinder) RequestBeanAnnotationBinder(io.micronaut.http.bind.binders.RequestBeanAnnotationBinder) BodyArgumentBinder(io.micronaut.http.bind.binders.BodyArgumentBinder) ArgumentBinder(io.micronaut.core.bind.ArgumentBinder) NonBlockingBodyArgumentBinder(io.micronaut.http.bind.binders.NonBlockingBodyArgumentBinder)

Aggregations

ArgumentBinder (io.micronaut.core.bind.ArgumentBinder)1 ArgumentConversionContext (io.micronaut.core.convert.ArgumentConversionContext)1 BodyArgumentBinder (io.micronaut.http.bind.binders.BodyArgumentBinder)1 NonBlockingBodyArgumentBinder (io.micronaut.http.bind.binders.NonBlockingBodyArgumentBinder)1 RequestBeanAnnotationBinder (io.micronaut.http.bind.binders.RequestBeanAnnotationBinder)1 UnresolvedArgument (io.micronaut.web.router.UnresolvedArgument)1 Optional (java.util.Optional)1