Skip to content

Commit

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

Signed-off-by: Yanming Zhou <[email protected]>
  • Loading branch information
quaff committed Jan 21, 2025
1 parent 2dabd11 commit 34aca9c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
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 @@ -44,6 +44,7 @@
*
* @author Stephane Nicoll
* @author Madhura Bhave
* @author Yanming Zhou
* @since 1.5.0
*/
@AutoConfiguration
Expand Down Expand Up @@ -73,6 +74,9 @@ public static MethodValidationPostProcessor methodValidationPostProcessor(Enviro
excludeFilters.orderedStream());
boolean proxyTargetClass = environment.getProperty("spring.aop.proxy-target-class", Boolean.class, true);
processor.setProxyTargetClass(proxyTargetClass);
boolean adaptConstraintViolations = environment.getProperty("spring.validation.adapt-constraint-violations",
Boolean.class, false);
processor.setAdaptConstraintViolations(adaptConstraintViolations);
processor.setValidatorProvider(validator);
return processor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2767,6 +2767,12 @@
"name": "spring.thymeleaf.suffix",
"defaultValue": ".html"
},
{
"name": "spring.validation.adapt-constraint-violations",
"type": "java.lang.Boolean",
"description": "Whether to adapt ConstraintViolations to MethodValidationResult.",
"defaultValue": false
},
{
"name": "spring.webflux.hiddenmethod.filter.enabled",
"type": "java.lang.Boolean",
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 @@ -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.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 34aca9c

Please sign in to comment.