Skip to content

Commit c20f6cb

Browse files
committed
Polish "Add spring.validation.method.adapt-constraint-violations property"
See gh-43886
1 parent 0e7d480 commit c20f6cb

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,13 @@ public static LocalValidatorFactoryBean defaultValidator(ApplicationContext appl
7171
@Bean
7272
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
7373
public static MethodValidationPostProcessor methodValidationPostProcessor(Environment environment,
74-
ObjectProvider<Validator> validator, ObjectProvider<MethodValidationExcludeFilter> excludeFilters,
75-
ObjectProvider<ValidationProperties> validationProperties) {
74+
ValidationProperties validationProperties, ObjectProvider<Validator> validator,
75+
ObjectProvider<MethodValidationExcludeFilter> excludeFilters) {
7676
FilteredMethodValidationPostProcessor processor = new FilteredMethodValidationPostProcessor(
7777
excludeFilters.orderedStream());
7878
boolean proxyTargetClass = environment.getProperty("spring.aop.proxy-target-class", Boolean.class, true);
7979
processor.setProxyTargetClass(proxyTargetClass);
80-
processor
81-
.setAdaptConstraintViolations(validationProperties.getObject().getMethod().isAdaptConstraintViolations());
80+
processor.setAdaptConstraintViolations(validationProperties.getMethod().isAdaptConstraintViolations());
8281
processor.setValidatorProvider(validator);
8382
return processor;
8483
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@
1616

1717
package org.springframework.boot.autoconfigure.validation;
1818

19+
import org.springframework.beans.factory.config.BeanDefinition;
1920
import org.springframework.boot.context.properties.ConfigurationProperties;
21+
import org.springframework.context.annotation.Role;
2022

2123
/**
22-
* Configuration properties for validation.
24+
* {@link ConfigurationProperties @ConfigurationProperties} for validation.
2325
*
2426
* @author Yanming Zhou
27+
* @author Andy Wilkinson
2528
* @since 3.5.0
2629
*/
30+
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
2731
@ConfigurationProperties(prefix = "spring.validation")
2832
public class ValidationProperties {
2933

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)