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);
}
Aggregations