Skip to content

Commit 44167f7

Browse files
committed
Merge pull request #43886 from quaff
* gh-43886: Polish "Add spring.validation.method.adapt-constraint-violations property" Add spring.validation.method.adapt-constraint-violations property Closes gh-43886
2 parents e7eca56 + c20f6cb commit 44167f7

File tree

5 files changed

+126
-4
lines changed

5 files changed

+126
-4
lines changed

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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 7 additions & 2 deletions
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,13 @@ 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+
ValidationProperties validationProperties, ObjectProvider<Validator> validator,
75+
ObjectProvider<MethodValidationExcludeFilter> excludeFilters) {
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.setAdaptConstraintViolations(validationProperties.getMethod().isAdaptConstraintViolations());
7681
processor.setValidatorProvider(validator);
7782
return processor;
7883
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.beans.factory.config.BeanDefinition;
20+
import org.springframework.boot.context.properties.ConfigurationProperties;
21+
import org.springframework.context.annotation.Role;
22+
23+
/**
24+
* {@link ConfigurationProperties @ConfigurationProperties} for validation.
25+
*
26+
* @author Yanming Zhou
27+
* @author Andy Wilkinson
28+
* @since 3.5.0
29+
*/
30+
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
31+
@ConfigurationProperties(prefix = "spring.validation")
32+
public class ValidationProperties {
33+
34+
private Method method = new Method();
35+
36+
public Method getMethod() {
37+
return this.method;
38+
}
39+
40+
public void setMethod(Method method) {
41+
this.method = method;
42+
}
43+
44+
/**
45+
* Method validation properties.
46+
*/
47+
public static class Method {
48+
49+
/**
50+
* Whether to adapt ConstraintViolations to MethodValidationResult.
51+
*/
52+
private boolean adaptConstraintViolations;
53+
54+
public boolean isAdaptConstraintViolations() {
55+
return this.adaptConstraintViolations;
56+
}
57+
58+
public void setAdaptConstraintViolations(boolean adaptConstraintViolations) {
59+
this.adaptConstraintViolations = adaptConstraintViolations;
60+
}
61+
62+
}
63+
64+
}

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.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() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.junit.jupiter.api.Test;
20+
21+
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
/**
26+
* Tests for {@link ValidationProperties}.
27+
*
28+
* @author Andy Wilkinson
29+
*/
30+
class ValidationPropertiesTests {
31+
32+
@Test
33+
void adaptConstraintViolationsPropertyDefaultMatchesPostProcessorDefault() {
34+
assertThat(new MethodValidationPostProcessor()).extracting("adaptConstraintViolations")
35+
.isEqualTo(new ValidationProperties().getMethod().isAdaptConstraintViolations());
36+
}
37+
38+
}

0 commit comments

Comments
 (0)