Add spring.validation.method.adapt-constraint-violations property
See gh-43886 Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
This commit is contained in:
parent
e7eca56dde
commit
0e7d480545
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
* Copyright 2012-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -101,6 +101,7 @@ public abstract class DocumentConfigurationProperties extends DefaultTask {
|
|||
config.accept("spring.ssl");
|
||||
config.accept("spring.task");
|
||||
config.accept("spring.threads");
|
||||
config.accept("spring.validation");
|
||||
config.accept("spring.mandatory-file-encoding");
|
||||
config.accept("info");
|
||||
config.accept("spring.output.ansi.enabled");
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
* Copyright 2012-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -27,6 +27,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
|
||||
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.validation.MessageInterpolatorFactory;
|
||||
import org.springframework.boot.validation.beanvalidation.FilteredMethodValidationPostProcessor;
|
||||
import org.springframework.boot.validation.beanvalidation.MethodValidationExcludeFilter;
|
||||
|
|
@ -44,11 +45,13 @@ import org.springframework.validation.beanvalidation.MethodValidationPostProcess
|
|||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Madhura Bhave
|
||||
* @author Yanming Zhou
|
||||
* @since 1.5.0
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@ConditionalOnClass(ExecutableValidator.class)
|
||||
@ConditionalOnResource(resources = "classpath:META-INF/services/jakarta.validation.spi.ValidationProvider")
|
||||
@EnableConfigurationProperties(ValidationProperties.class)
|
||||
@Import(PrimaryDefaultValidatorPostProcessor.class)
|
||||
public class ValidationAutoConfiguration {
|
||||
|
||||
|
|
@ -68,11 +71,14 @@ public class ValidationAutoConfiguration {
|
|||
@Bean
|
||||
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
|
||||
public static MethodValidationPostProcessor methodValidationPostProcessor(Environment environment,
|
||||
ObjectProvider<Validator> validator, ObjectProvider<MethodValidationExcludeFilter> excludeFilters) {
|
||||
ObjectProvider<Validator> validator, ObjectProvider<MethodValidationExcludeFilter> excludeFilters,
|
||||
ObjectProvider<ValidationProperties> validationProperties) {
|
||||
FilteredMethodValidationPostProcessor processor = new FilteredMethodValidationPostProcessor(
|
||||
excludeFilters.orderedStream());
|
||||
boolean proxyTargetClass = environment.getProperty("spring.aop.proxy-target-class", Boolean.class, true);
|
||||
processor.setProxyTargetClass(proxyTargetClass);
|
||||
processor
|
||||
.setAdaptConstraintViolations(validationProperties.getObject().getMethod().isAdaptConstraintViolations());
|
||||
processor.setValidatorProvider(validator);
|
||||
return processor;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Copyright 2012-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.validation;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* Configuration properties for validation.
|
||||
*
|
||||
* @author Yanming Zhou
|
||||
* @since 3.5.0
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "spring.validation")
|
||||
public class ValidationProperties {
|
||||
|
||||
private Method method = new Method();
|
||||
|
||||
public Method getMethod() {
|
||||
return this.method;
|
||||
}
|
||||
|
||||
public void setMethod(Method method) {
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method validation properties.
|
||||
*/
|
||||
public static class Method {
|
||||
|
||||
/**
|
||||
* Whether to adapt ConstraintViolations to MethodValidationResult.
|
||||
*/
|
||||
private boolean adaptConstraintViolations;
|
||||
|
||||
public boolean isAdaptConstraintViolations() {
|
||||
return this.adaptConstraintViolations;
|
||||
}
|
||||
|
||||
public void setAdaptConstraintViolations(boolean adaptConstraintViolations) {
|
||||
this.adaptConstraintViolations = adaptConstraintViolations;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
* Copyright 2012-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -46,6 +46,7 @@ import org.springframework.validation.beanvalidation.CustomValidatorBean;
|
|||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
|
||||
import org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean;
|
||||
import org.springframework.validation.method.MethodValidationException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
|
@ -59,6 +60,7 @@ import static org.mockito.Mockito.mock;
|
|||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Phillip Webb
|
||||
* @author Yanming Zhou
|
||||
*/
|
||||
class ValidationAutoConfigurationTests {
|
||||
|
||||
|
|
@ -207,6 +209,18 @@ class ValidationAutoConfigurationTests {
|
|||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void validationCanBeConfiguredToAdaptConstraintViolations() {
|
||||
this.contextRunner.withUserConfiguration(AnotherSampleServiceConfiguration.class)
|
||||
.withPropertyValues("spring.validation.method.adapt-constraint-violations=true")
|
||||
.run((context) -> {
|
||||
assertThat(context.getBeansOfType(Validator.class)).hasSize(1);
|
||||
AnotherSampleService service = context.getBean(AnotherSampleService.class);
|
||||
service.doSomething(42);
|
||||
assertThatExceptionOfType(MethodValidationException.class).isThrownBy(() -> service.doSomething(2));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void userDefinedMethodValidationPostProcessorTakesPrecedence() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue