Skip to content

Commit 34aca9c

Browse files
committed
Add spring.validation.adapt-constraint-violations property
Closes GH-43881 Signed-off-by: Yanming Zhou <[email protected]>
1 parent 2dabd11 commit 34aca9c

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

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

Lines changed: 5 additions & 1 deletion
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.
@@ -44,6 +44,7 @@
4444
*
4545
* @author Stephane Nicoll
4646
* @author Madhura Bhave
47+
* @author Yanming Zhou
4748
* @since 1.5.0
4849
*/
4950
@AutoConfiguration
@@ -73,6 +74,9 @@ public static MethodValidationPostProcessor methodValidationPostProcessor(Enviro
7374
excludeFilters.orderedStream());
7475
boolean proxyTargetClass = environment.getProperty("spring.aop.proxy-target-class", Boolean.class, true);
7576
processor.setProxyTargetClass(proxyTargetClass);
77+
boolean adaptConstraintViolations = environment.getProperty("spring.validation.adapt-constraint-violations",
78+
Boolean.class, false);
79+
processor.setAdaptConstraintViolations(adaptConstraintViolations);
7680
processor.setValidatorProvider(validator);
7781
return processor;
7882
}

spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2767,6 +2767,12 @@
27672767
"name": "spring.thymeleaf.suffix",
27682768
"defaultValue": ".html"
27692769
},
2770+
{
2771+
"name": "spring.validation.adapt-constraint-violations",
2772+
"type": "java.lang.Boolean",
2773+
"description": "Whether to adapt ConstraintViolations to MethodValidationResult.",
2774+
"defaultValue": false
2775+
},
27702776
{
27712777
"name": "spring.webflux.hiddenmethod.filter.enabled",
27722778
"type": "java.lang.Boolean",

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

Lines changed: 15 additions & 1 deletion
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.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)