Polish validation in RequestBodyArgumentResolver
This commit is contained in:
@@ -18,9 +18,9 @@ package org.springframework.web.reactive.result.method.annotation;
|
|||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.reactivestreams.Publisher;
|
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
@@ -137,16 +137,16 @@ public class RequestBodyArgumentResolver implements HandlerMethodArgumentResolve
|
|||||||
for (HttpMessageConverter<?> converter : getMessageConverters()) {
|
for (HttpMessageConverter<?> converter : getMessageConverters()) {
|
||||||
if (converter.canRead(elementType, mediaType)) {
|
if (converter.canRead(elementType, mediaType)) {
|
||||||
if (convertFromFlux) {
|
if (convertFromFlux) {
|
||||||
Publisher<?> flux = converter.read(elementType, request);
|
Flux<?> flux = converter.read(elementType, request);
|
||||||
if (this.validator != null) {
|
if (this.validator != null) {
|
||||||
flux= applyValidationIfApplicable(flux, parameter);
|
flux = flux.map(applyValidationIfApplicable(parameter));
|
||||||
}
|
}
|
||||||
return Mono.just(this.conversionService.convert(flux, type.getRawClass()));
|
return Mono.just(this.conversionService.convert(flux, type.getRawClass()));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Mono<?> mono = converter.readOne(elementType, request);
|
Mono<?> mono = converter.readOne(elementType, request);
|
||||||
if (this.validator != null) {
|
if (this.validator != null) {
|
||||||
mono = Mono.from(applyValidationIfApplicable(mono, parameter));
|
mono = mono.map(applyValidationIfApplicable(parameter));
|
||||||
}
|
}
|
||||||
if (!convertFromMono) {
|
if (!convertFromMono) {
|
||||||
return mono.map(value-> value); // TODO: MonoToObjectConverter
|
return mono.map(value-> value); // TODO: MonoToObjectConverter
|
||||||
@@ -159,20 +159,20 @@ public class RequestBodyArgumentResolver implements HandlerMethodArgumentResolve
|
|||||||
return Mono.error(new UnsupportedMediaTypeStatusException(mediaType, this.supportedMediaTypes));
|
return Mono.error(new UnsupportedMediaTypeStatusException(mediaType, this.supportedMediaTypes));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Publisher<?> applyValidationIfApplicable(Publisher<?> elements, MethodParameter methodParam) {
|
protected <T> Function<T, T> applyValidationIfApplicable(MethodParameter methodParam) {
|
||||||
Annotation[] annotations = methodParam.getParameterAnnotations();
|
Annotation[] annotations = methodParam.getParameterAnnotations();
|
||||||
for (Annotation ann : annotations) {
|
for (Annotation ann : annotations) {
|
||||||
Validated validAnnot = AnnotationUtils.getAnnotation(ann, Validated.class);
|
Validated validAnnot = AnnotationUtils.getAnnotation(ann, Validated.class);
|
||||||
if (validAnnot != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
|
if (validAnnot != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
|
||||||
Object hints = (validAnnot != null ? validAnnot.value() : AnnotationUtils.getValue(ann));
|
Object hints = (validAnnot != null ? validAnnot.value() : AnnotationUtils.getValue(ann));
|
||||||
Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
|
Object[] validHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
|
||||||
return Flux.from(elements).map(element -> {
|
return element -> {
|
||||||
doValidate(element, validationHints, methodParam);
|
doValidate(element, validHints, methodParam);
|
||||||
return element;
|
return element;
|
||||||
});
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return elements;
|
return element -> element;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user