Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add spring.validation.method.adapt-constraint-violations property #43886

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
quaff marked this conversation as resolved.
Show resolved Hide resolved
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,14 @@ 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,
ObjectProvider<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.getObject().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
Loading