|
|
|
@ -16,10 +16,11 @@ |
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.web.reactive; |
|
|
|
package org.springframework.web.reactive; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Method; |
|
|
|
|
|
|
|
|
|
|
|
import jakarta.validation.Valid; |
|
|
|
import jakarta.validation.Valid; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.core.MethodParameter; |
|
|
|
|
|
|
|
import org.springframework.core.ResolvableType; |
|
|
|
import org.springframework.core.ResolvableType; |
|
|
|
import org.springframework.validation.Errors; |
|
|
|
import org.springframework.validation.Errors; |
|
|
|
import org.springframework.validation.SmartValidator; |
|
|
|
import org.springframework.validation.SmartValidator; |
|
|
|
@ -27,7 +28,6 @@ import org.springframework.validation.Validator; |
|
|
|
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; |
|
|
|
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; |
|
|
|
import org.springframework.web.bind.WebDataBinder; |
|
|
|
import org.springframework.web.bind.WebDataBinder; |
|
|
|
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest; |
|
|
|
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest; |
|
|
|
import org.springframework.web.testfixture.method.ResolvableMethod; |
|
|
|
|
|
|
|
import org.springframework.web.testfixture.server.MockServerWebExchange; |
|
|
|
import org.springframework.web.testfixture.server.MockServerWebExchange; |
|
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
@ -40,44 +40,37 @@ import static org.mockito.BDDMockito.when; |
|
|
|
class BindingContextTests { |
|
|
|
class BindingContextTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void jakartaValidatorExcludedWhenMethodValidationApplicable() { |
|
|
|
void jakartaValidatorExcludedWhenMethodValidationApplicable() throws Exception { |
|
|
|
BindingContext bindingContext = new BindingContext(null); |
|
|
|
BindingContext bindingContext = new BindingContext(null); |
|
|
|
bindingContext.setMethodValidationApplicable(true); |
|
|
|
bindingContext.setMethodValidationApplicable(true); |
|
|
|
|
|
|
|
|
|
|
|
MethodParameter parameter = ResolvableMethod.on(BindingContextTests.class) |
|
|
|
Method method = getClass().getDeclaredMethod("handleValidObject", Foo.class); |
|
|
|
.named("handle").build().annotPresent(Valid.class).arg(); |
|
|
|
ResolvableType targetType = ResolvableType.forMethodParameter(method, 0); |
|
|
|
|
|
|
|
|
|
|
|
WebDataBinder dataBinder = bindingContext.createDataBinder( |
|
|
|
WebDataBinder binder = bindingContext.createDataBinder( |
|
|
|
MockServerWebExchange.from(MockServerHttpRequest.get("")), new Foo(), "foo", |
|
|
|
MockServerWebExchange.from(MockServerHttpRequest.get("")), new Foo(), "foo", targetType); |
|
|
|
ResolvableType.forMethodParameter(parameter)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Validator springValidator = mock(Validator.class); |
|
|
|
Validator springValidator = mock(Validator.class); |
|
|
|
when(springValidator.supports(Foo.class)).thenReturn(true); |
|
|
|
when(springValidator.supports(Foo.class)).thenReturn(true); |
|
|
|
dataBinder.addValidators(springValidator); |
|
|
|
binder.addValidators(springValidator); |
|
|
|
|
|
|
|
|
|
|
|
LocalValidatorFactoryBean beanValidator = new LocalValidatorFactoryBean(); |
|
|
|
LocalValidatorFactoryBean beanValidator = new LocalValidatorFactoryBean(); |
|
|
|
beanValidator.afterPropertiesSet(); |
|
|
|
beanValidator.afterPropertiesSet(); |
|
|
|
dataBinder.addValidators(beanValidator); |
|
|
|
binder.addValidators(beanValidator); |
|
|
|
|
|
|
|
|
|
|
|
WrappedBeanValidator wrappedBeanValidator = new WrappedBeanValidator(beanValidator); |
|
|
|
WrappedBeanValidator wrappedBeanValidator = new WrappedBeanValidator(beanValidator); |
|
|
|
dataBinder.addValidators(wrappedBeanValidator); |
|
|
|
binder.addValidators(wrappedBeanValidator); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(dataBinder.getValidatorsToApply()).containsExactly(springValidator); |
|
|
|
assertThat(binder.getValidatorsToApply()).containsExactly(springValidator); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unused") |
|
|
|
@SuppressWarnings("unused") |
|
|
|
private void handle(@Valid Foo foo) { |
|
|
|
private void handleValidObject(@Valid Foo foo) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class WrappedBeanValidator implements SmartValidator { |
|
|
|
private record WrappedBeanValidator(jakarta.validation.Validator validator) implements SmartValidator { |
|
|
|
|
|
|
|
|
|
|
|
private final jakarta.validation.Validator validator; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private WrappedBeanValidator(jakarta.validation.Validator validator) { |
|
|
|
|
|
|
|
this.validator = validator; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public boolean supports(Class<?> clazz) { |
|
|
|
public boolean supports(Class<?> clazz) { |
|
|
|
@ -100,6 +93,6 @@ class BindingContextTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class Foo {} |
|
|
|
private static final class Foo {} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|