Commit 7bcb5c44 authored by Phillip Webb's avatar Phillip Webb

Merge pull request #12669 from geo-m

* pr/12669:
  Polish "Allow validation api without implementation"
  Allow validation api without implementation
parents b67e6aaf 0c98d0e7
...@@ -55,7 +55,9 @@ class ConfigurationPropertiesBinder { ...@@ -55,7 +55,9 @@ class ConfigurationPropertiesBinder {
private final Validator configurationPropertiesValidator; private final Validator configurationPropertiesValidator;
private final Validator jsr303Validator; private final boolean jsr303Present;
private volatile Validator jsr303Validator;
private volatile Binder binder; private volatile Binder binder;
...@@ -66,8 +68,8 @@ class ConfigurationPropertiesBinder { ...@@ -66,8 +68,8 @@ class ConfigurationPropertiesBinder {
.getPropertySources(); .getPropertySources();
this.configurationPropertiesValidator = getConfigurationPropertiesValidator( this.configurationPropertiesValidator = getConfigurationPropertiesValidator(
applicationContext, validatorBeanName); applicationContext, validatorBeanName);
this.jsr303Validator = ConfigurationPropertiesJsr303Validator this.jsr303Present = ConfigurationPropertiesJsr303Validator
.getIfJsr303Present(applicationContext); .isJsr303Present(applicationContext);
} }
public void bind(Bindable<?> target) { public void bind(Bindable<?> target) {
...@@ -93,9 +95,8 @@ class ConfigurationPropertiesBinder { ...@@ -93,9 +95,8 @@ class ConfigurationPropertiesBinder {
if (this.configurationPropertiesValidator != null) { if (this.configurationPropertiesValidator != null) {
validators.add(this.configurationPropertiesValidator); validators.add(this.configurationPropertiesValidator);
} }
if (this.jsr303Validator != null if (this.jsr303Present && target.getAnnotation(Validated.class) != null) {
&& target.getAnnotation(Validated.class) != null) { validators.add(getJsr303Validator());
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());
...@@ -103,6 +104,14 @@ class ConfigurationPropertiesBinder { ...@@ -103,6 +104,14 @@ class ConfigurationPropertiesBinder {
return validators; return validators;
} }
private Validator getJsr303Validator() {
if (this.jsr303Validator == null) {
this.jsr303Validator = new ConfigurationPropertiesJsr303Validator(
this.applicationContext);
}
return this.jsr303Validator;
}
private BindHandler getBindHandler(ConfigurationProperties annotation, private BindHandler getBindHandler(ConfigurationProperties annotation,
List<Validator> validators) { List<Validator> validators) {
BindHandler handler = new IgnoreTopLevelConverterNotFoundBindHandler(); BindHandler handler = new IgnoreTopLevelConverterNotFoundBindHandler();
......
...@@ -38,8 +38,7 @@ final class ConfigurationPropertiesJsr303Validator implements Validator { ...@@ -38,8 +38,7 @@ final class ConfigurationPropertiesJsr303Validator implements Validator {
private final Delegate delegate; private final Delegate delegate;
private ConfigurationPropertiesJsr303Validator( ConfigurationPropertiesJsr303Validator(ApplicationContext applicationContext) {
ApplicationContext applicationContext) {
this.delegate = new Delegate(applicationContext); this.delegate = new Delegate(applicationContext);
} }
...@@ -53,15 +52,14 @@ final class ConfigurationPropertiesJsr303Validator implements Validator { ...@@ -53,15 +52,14 @@ final class ConfigurationPropertiesJsr303Validator implements Validator {
this.delegate.validate(target, errors); this.delegate.validate(target, errors);
} }
public static ConfigurationPropertiesJsr303Validator getIfJsr303Present( public static boolean isJsr303Present(ApplicationContext applicationContext) {
ApplicationContext applicationContext) {
ClassLoader classLoader = applicationContext.getClassLoader(); ClassLoader classLoader = applicationContext.getClassLoader();
for (String validatorClass : VALIDATOR_CLASSES) { for (String validatorClass : VALIDATOR_CLASSES) {
if (!ClassUtils.isPresent(validatorClass, classLoader)) { if (!ClassUtils.isPresent(validatorClass, classLoader)) {
return null; return false;
} }
} }
return new ConfigurationPropertiesJsr303Validator(applicationContext); return true;
} }
private static class Delegate extends LocalValidatorFactoryBean { private static class Delegate extends LocalValidatorFactoryBean {
......
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -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,12 @@ public class ValidationExceptionFailureAnalyzerTests { ...@@ -51,6 +52,12 @@ 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 +67,22 @@ public class ValidationExceptionFailureAnalyzerTests { ...@@ -60,8 +67,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