Skip to content

Commit

Permalink
Add spring.validation.adapt-constraint-violations property
Browse files Browse the repository at this point in the history
Closes spring-projectsGH-43881

Signed-off-by: Yanming Zhou <[email protected]>
  • Loading branch information
quaff committed Jan 22, 2025
1 parent 91da8c5 commit 47f398a
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -101,6 +101,7 @@ private void corePrefixes(Config config) {
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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -27,6 +27,7 @@
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;
Expand All @@ -44,11 +45,13 @@
*
* @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 {

Expand All @@ -68,11 +71,13 @@ public static LocalValidatorFactoryBean defaultValidator(ApplicationContext appl
@Bean
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
public static MethodValidationPostProcessor methodValidationPostProcessor(Environment environment,
ObjectProvider<Validator> validator, ObjectProvider<MethodValidationExcludeFilter> excludeFilters) {
ObjectProvider<Validator> validator, ObjectProvider<MethodValidationExcludeFilter> excludeFilters,
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.getMethod().isAdaptConstraintViolations());
processor.setValidatorProvider(validator);
return processor;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -46,6 +46,7 @@
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;
Expand All @@ -59,6 +60,7 @@
*
* @author Stephane Nicoll
* @author Phillip Webb
* @author Yanming Zhou
*/
class ValidationAutoConfigurationTests {

Expand Down Expand Up @@ -207,6 +209,18 @@ void validationCanBeConfiguredToUseJdkProxy() {
});
}

@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() {
Expand Down

0 comments on commit 47f398a

Please sign in to comment.