use of io.micronaut.core.convert.ArgumentConversionContext in project micronaut-core by micronaut-projects.
the class BeanWrapper method getRequiredProperty.
/**
* Get the property value of the given type or throw an exception if it is unobtainable.
* @param name The name
* @param context The type
* @param <P> The property generic type
* @return The property value
* @throws IntrospectionException if no property exists
* @throws ConversionErrorException if the property cannot be converted to the given type
*/
@NonNull
default <P> P getRequiredProperty(@NonNull String name, @NonNull ArgumentConversionContext<P> context) {
ArgumentUtils.requireNonNull("name", name);
ArgumentUtils.requireNonNull("type", context);
return getIntrospection().getProperty(name).map(prop -> {
final Optional<P> converted = prop.get(getBean(), context);
return converted.orElseThrow(() -> {
final ConversionError conversionError = context.getLastError().orElseGet(() -> new ConversionError() {
@Override
public Exception getCause() {
return new IntrospectionException("Property of type [" + prop.getType() + "] cannot be converted to type: " + context.getArgument().getType());
}
@Override
public Optional<Object> getOriginalValue() {
return Optional.ofNullable(prop.get(getBean()));
}
});
return new ConversionErrorException(context.getArgument(), conversionError);
});
}).orElseThrow(() -> new IntrospectionException("No property found for name: " + name));
}
use of io.micronaut.core.convert.ArgumentConversionContext 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