Skip to content

Commit 0e7d480

Browse files
quaffwilkinsona
authored andcommitted
Add spring.validation.method.adapt-constraint-violations property
See gh-43886 Signed-off-by: Yanming Zhou <[email protected]>
1 parent e7eca56 commit 0e7d480

File tree

4 files changed

+85
-4
lines changed

4 files changed

+85
-4
lines changed

buildSrc/src/main/java/org/springframework/boot/build/context/properties/DocumentConfigurationProperties.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -101,6 +101,7 @@ private void corePrefixes(Config config) {
101101
config.accept("spring.ssl");
102102
config.accept("spring.task");
103103
config.accept("spring.threads");
104+
config.accept("spring.validation");
104105
config.accept("spring.mandatory-file-encoding");
105106
config.accept("info");
106107
config.accept("spring.output.ansi.enabled");

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2828
import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
2929
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
30+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3031
import org.springframework.boot.validation.MessageInterpolatorFactory;
3132
import org.springframework.boot.validation.beanvalidation.FilteredMethodValidationPostProcessor;
3233
import org.springframework.boot.validation.beanvalidation.MethodValidationExcludeFilter;
@@ -44,11 +45,13 @@
4445
*
4546
* @author Stephane Nicoll
4647
* @author Madhura Bhave
48+
* @author Yanming Zhou
4749
* @since 1.5.0
4850
*/
4951
@AutoConfiguration
5052
@ConditionalOnClass(ExecutableValidator.class)
5153
@ConditionalOnResource(resources = "classpath:META-INF/services/jakarta.validation.spi.ValidationProvider")
54+
@EnableConfigurationProperties(ValidationProperties.class)
5255
@Import(PrimaryDefaultValidatorPostProcessor.class)
5356
public class ValidationAutoConfiguration {
5457

@@ -68,11 +71,14 @@ public static LocalValidatorFactoryBean defaultValidator(ApplicationContext appl
6871
@Bean
6972
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
7073
public static MethodValidationPostProcessor methodValidationPostProcessor(Environment environment,
71-
ObjectProvider<Validator> validator, ObjectProvider<MethodValidationExcludeFilter> excludeFilters) {
74+
ObjectProvider<Validator> validator, ObjectProvider<MethodValidationExcludeFilter> excludeFilters,
75+
ObjectProvider<ValidationProperties> validationProperties) {
7276
FilteredMethodValidationPostProcessor processor = new FilteredMethodValidationPostProcessor(
7377
excludeFilters.orderedStream());
7478
boolean proxyTargetClass = environment.getProperty("spring.aop.proxy-target-class", Boolean.class, true);
7579
processor.setProxyTargetClass(proxyTargetClass);
80+
processor
81+
.setAdaptConstraintViolations(validationProperties.getObject().getMethod().isAdaptConstraintViolations());
7682
processor.setValidatorProvider(validator);
7783
return processor;
7884
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.validation;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
21+
/**
22+
* Configuration properties for validation.
23+
*
24+
* @author Yanming Zhou
25+
* @since 3.5.0
26+
*/
27+
@ConfigurationProperties(prefix = "spring.validation")
28+
public class ValidationProperties {
29+
30+
private Method method = new Method();
31+
32+
public Method getMethod() {
33+
return this.method;
34+
}
35+
36+
public void setMethod(Method method) {
37+
this.method = method;
38+
}
39+
40+
/**
41+
* Method validation properties.
42+
*/
43+
public static class Method {
44+
45+
/**
46+
* Whether to adapt ConstraintViolations to MethodValidationResult.
47+
*/
48+
private boolean adaptConstraintViolations;
49+
50+
public boolean isAdaptConstraintViolations() {
51+
return this.adaptConstraintViolations;
52+
}
53+
54+
public void setAdaptConstraintViolations(boolean adaptConstraintViolations) {
55+
this.adaptConstraintViolations = adaptConstraintViolations;
56+
}
57+
58+
}
59+
60+
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfigurationTests.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,6 +46,7 @@
4646
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
4747
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
4848
import org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean;
49+
import org.springframework.validation.method.MethodValidationException;
4950

5051
import static org.assertj.core.api.Assertions.assertThat;
5152
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -59,6 +60,7 @@
5960
*
6061
* @author Stephane Nicoll
6162
* @author Phillip Webb
63+
* @author Yanming Zhou
6264
*/
6365
class ValidationAutoConfigurationTests {
6466

@@ -207,6 +209,18 @@ void validationCanBeConfiguredToUseJdkProxy() {
207209
});
208210
}
209211

212+
@Test
213+
void validationCanBeConfiguredToAdaptConstraintViolations() {
214+
this.contextRunner.withUserConfiguration(AnotherSampleServiceConfiguration.class)
215+
.withPropertyValues("spring.validation.method.adapt-constraint-violations=true")
216+
.run((context) -> {
217+
assertThat(context.getBeansOfType(Validator.class)).hasSize(1);
218+
AnotherSampleService service = context.getBean(AnotherSampleService.class);
219+
service.doSomething(42);
220+
assertThatExceptionOfType(MethodValidationException.class).isThrownBy(() -> service.doSomething(2));
221+
});
222+
}
223+
210224
@Test
211225
@SuppressWarnings("unchecked")
212226
void userDefinedMethodValidationPostProcessorTakesPrecedence() {

0 commit comments

Comments
 (0)