Commit a74dc74e authored by geo_m's avatar geo_m Committed by Phillip Webb

Allow validation api without implementation

Allow validation api in the classpath without
a validator, as long as no bean is annotated `@Validated`

See gh-12669
parent b67e6aaf
...@@ -55,7 +55,7 @@ class ConfigurationPropertiesBinder { ...@@ -55,7 +55,7 @@ class ConfigurationPropertiesBinder {
private final Validator configurationPropertiesValidator; private final Validator configurationPropertiesValidator;
private final Validator jsr303Validator; private Validator jsr303Validator;
private volatile Binder binder; private volatile Binder binder;
...@@ -66,8 +66,7 @@ class ConfigurationPropertiesBinder { ...@@ -66,8 +66,7 @@ class ConfigurationPropertiesBinder {
.getPropertySources(); .getPropertySources();
this.configurationPropertiesValidator = getConfigurationPropertiesValidator( this.configurationPropertiesValidator = getConfigurationPropertiesValidator(
applicationContext, validatorBeanName); applicationContext, validatorBeanName);
this.jsr303Validator = ConfigurationPropertiesJsr303Validator
.getIfJsr303Present(applicationContext);
} }
public void bind(Bindable<?> target) { public void bind(Bindable<?> target) {
...@@ -93,9 +92,12 @@ class ConfigurationPropertiesBinder { ...@@ -93,9 +92,12 @@ class ConfigurationPropertiesBinder {
if (this.configurationPropertiesValidator != null) { if (this.configurationPropertiesValidator != null) {
validators.add(this.configurationPropertiesValidator); validators.add(this.configurationPropertiesValidator);
} }
if (this.jsr303Validator != null
&& target.getAnnotation(Validated.class) != null) { if (target.getAnnotation(Validated.class) != null) {
validators.add(this.jsr303Validator); this.jsr303Validator = ConfigurationPropertiesJsr303Validator.getIfJsr303Present(this.applicationContext);
if (this.jsr303Validator != null) {
validators.add(this.jsr303Validator);
}
} }
if (target.getValue() != null && target.getValue().get() instanceof Validator) { if (target.getValue() != null && target.getValue().get() instanceof Validator) {
validators.add((Validator) target.getValue().get()); validators.add((Validator) target.getValue().get());
......
...@@ -25,6 +25,7 @@ import org.springframework.boot.diagnostics.FailureAnalysis; ...@@ -25,6 +25,7 @@ import org.springframework.boot.diagnostics.FailureAnalysis;
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.validation.annotation.Validated;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
...@@ -39,7 +40,7 @@ import static org.junit.Assert.fail; ...@@ -39,7 +40,7 @@ import static org.junit.Assert.fail;
public class ValidationExceptionFailureAnalyzerTests { public class ValidationExceptionFailureAnalyzerTests {
@Test @Test
public void test() { public void validatedPropertiesTest() {
try { try {
new AnnotationConfigApplicationContext(TestConfiguration.class).close(); new AnnotationConfigApplicationContext(TestConfiguration.class).close();
fail("Expected failure did not occur"); fail("Expected failure did not occur");
...@@ -51,6 +52,11 @@ public class ValidationExceptionFailureAnalyzerTests { ...@@ -51,6 +52,11 @@ public class ValidationExceptionFailureAnalyzerTests {
} }
} }
@Test
public void nonValidatedPropertiesTest() {
new AnnotationConfigApplicationContext(NonValidatedTestConfiguration.class).close();
}
@EnableConfigurationProperties(TestProperties.class) @EnableConfigurationProperties(TestProperties.class)
static class TestConfiguration { static class TestConfiguration {
...@@ -60,8 +66,22 @@ public class ValidationExceptionFailureAnalyzerTests { ...@@ -60,8 +66,22 @@ public class ValidationExceptionFailureAnalyzerTests {
} }
@ConfigurationProperties("test") @ConfigurationProperties("test")
@Validated
private static class TestProperties { private static class TestProperties {
} }
@EnableConfigurationProperties(NonValidatedTestProperties.class)
static class NonValidatedTestConfiguration {
NonValidatedTestConfiguration(NonValidatedTestProperties testProperties) {
}
}
@ConfigurationProperties("test")
private static class NonValidatedTestProperties {
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment