use of org.springframework.core.ReactiveAdapter in project spring-framework by spring-projects.
the class ErrorsMethodArgumentResolver method getModelAttributeName.
private String getModelAttributeName(MethodParameter parameter) {
Assert.isTrue(parameter.getParameterIndex() > 0, "Errors argument must be immediately after a model attribute argument.");
int index = parameter.getParameterIndex() - 1;
MethodParameter attributeParam = new MethodParameter(parameter.getMethod(), index);
Class<?> attributeType = attributeParam.getParameterType();
ResolvableType type = ResolvableType.forMethodParameter(attributeParam);
ReactiveAdapter adapter = getAdapterRegistry().getAdapter(type.resolve());
Assert.isNull(adapter, "Errors/BindingResult cannot be used with an async model attribute. " + "Either declare the model attribute without the async wrapper type " + "or handle WebExchangeBindException through the async type.");
ModelAttribute annot = parameter.getParameterAnnotation(ModelAttribute.class);
if (annot != null && StringUtils.hasText(annot.value())) {
return annot.value();
}
// TODO: Conventions does not deal with async wrappers
return ClassUtils.getShortNameAsProperty(attributeType);
}
use of org.springframework.core.ReactiveAdapter in project spring-framework by spring-projects.
the class ModelAttributeMethodArgumentResolver method getAttributeMono.
private Mono<?> getAttributeMono(String attributeName, ResolvableType attributeType, Model model) {
Object attribute = model.asMap().get(attributeName);
if (attribute == null) {
attribute = BeanUtils.instantiateClass(attributeType.getRawClass());
}
ReactiveAdapter adapterFrom = getAdapterRegistry().getAdapter(null, attribute);
if (adapterFrom != null) {
Assert.isTrue(!adapterFrom.isMultiValue(), "Data binding supports single-value async types.");
return Mono.from(adapterFrom.toPublisher(attribute));
} else {
return Mono.justOrEmpty(attribute);
}
}
use of org.springframework.core.ReactiveAdapter in project spring-framework by spring-projects.
the class HandlerMethodArgumentResolverSupport method checkParamTypeNoReactiveWrapper.
/**
* Evaluate the {@code Predicate} on the method parameter type but raise an
* {@code IllegalStateException} if the same matches the generic type
* within a reactive type wrapper.
*/
protected boolean checkParamTypeNoReactiveWrapper(MethodParameter param, Predicate<Class<?>> predicate) {
Class<?> type = param.getParameterType();
ReactiveAdapter adapter = getAdapterRegistry().getAdapter(type);
if (adapter != null) {
assertHasValues(adapter, param);
type = param.nested().getNestedParameterType();
}
if (predicate.test(type)) {
if (adapter == null) {
return true;
}
throw getReactiveWrapperError(param);
}
return false;
}
use of org.springframework.core.ReactiveAdapter in project spring-framework by spring-projects.
the class ViewResolutionResultHandler method resolveAsyncAttributes.
private Mono<Void> resolveAsyncAttributes(Map<String, Object> model) {
List<String> names = new ArrayList<>();
List<Mono<?>> valueMonos = new ArrayList<>();
for (Map.Entry<String, ?> entry : model.entrySet()) {
ReactiveAdapter adapter = getAdapterRegistry().getAdapter(null, entry.getValue());
if (adapter != null) {
names.add(entry.getKey());
if (adapter.isMultiValue()) {
Flux<Object> value = Flux.from(adapter.toPublisher(entry.getValue()));
valueMonos.add(value.collectList().defaultIfEmpty(Collections.emptyList()));
} else {
Mono<Object> value = Mono.from(adapter.toPublisher(entry.getValue()));
valueMonos.add(value.defaultIfEmpty(NO_VALUE));
}
}
}
if (names.isEmpty()) {
return Mono.empty();
}
return Mono.when(valueMonos, values -> {
for (int i = 0; i < values.length; i++) {
if (values[i] != NO_VALUE) {
model.put(names.get(i), values[i]);
} else {
model.remove(names.get(i));
}
}
return NO_VALUE;
}).then();
}
use of org.springframework.core.ReactiveAdapter in project spring-framework by spring-projects.
the class ReactiveAdapterRegistryTests method getAdapterForReactiveSubType.
@Test
public void getAdapterForReactiveSubType() throws Exception {
ReactiveAdapter adapter1 = getAdapter(Flux.class);
ReactiveAdapter adapter2 = getAdapter(FluxProcessor.class);
assertSame(adapter1, adapter2);
this.registry.registerReactiveType(ReactiveTypeDescriptor.multiValue(FluxProcessor.class, FluxProcessor::empty), o -> (FluxProcessor<?, ?>) o, FluxProcessor::from);
ReactiveAdapter adapter3 = getAdapter(FluxProcessor.class);
assertNotNull(adapter3);
assertNotSame(adapter1, adapter3);
}
Aggregations