Skip to content

Commit

Permalink
Merge pull request spring-projects#12669 from geo-m
Browse files Browse the repository at this point in the history
* pr/12669:
  Polish "Allow validation api without implementation"
  Allow validation api without implementation
  • Loading branch information
philwebb committed Apr 5, 2018
2 parents b67e6aa + 0c98d0e commit 7bcb5c4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ class ConfigurationPropertiesBinder {

private final Validator configurationPropertiesValidator;

private final Validator jsr303Validator;
private final boolean jsr303Present;

private volatile Validator jsr303Validator;

private volatile Binder binder;

Expand All @@ -66,8 +68,8 @@ class ConfigurationPropertiesBinder {
.getPropertySources();
this.configurationPropertiesValidator = getConfigurationPropertiesValidator(
applicationContext, validatorBeanName);
this.jsr303Validator = ConfigurationPropertiesJsr303Validator
.getIfJsr303Present(applicationContext);
this.jsr303Present = ConfigurationPropertiesJsr303Validator
.isJsr303Present(applicationContext);
}

public void bind(Bindable<?> target) {
Expand All @@ -93,16 +95,23 @@ private List<Validator> getValidators(Bindable<?> target) {
if (this.configurationPropertiesValidator != null) {
validators.add(this.configurationPropertiesValidator);
}
if (this.jsr303Validator != null
&& target.getAnnotation(Validated.class) != null) {
validators.add(this.jsr303Validator);
if (this.jsr303Present && target.getAnnotation(Validated.class) != null) {
validators.add(getJsr303Validator());
}
if (target.getValue() != null && target.getValue().get() instanceof Validator) {
validators.add((Validator) target.getValue().get());
}
return validators;
}

private Validator getJsr303Validator() {
if (this.jsr303Validator == null) {
this.jsr303Validator = new ConfigurationPropertiesJsr303Validator(
this.applicationContext);
}
return this.jsr303Validator;
}

private BindHandler getBindHandler(ConfigurationProperties annotation,
List<Validator> validators) {
BindHandler handler = new IgnoreTopLevelConverterNotFoundBindHandler();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ final class ConfigurationPropertiesJsr303Validator implements Validator {

private final Delegate delegate;

private ConfigurationPropertiesJsr303Validator(
ApplicationContext applicationContext) {
ConfigurationPropertiesJsr303Validator(ApplicationContext applicationContext) {
this.delegate = new Delegate(applicationContext);
}

Expand All @@ -53,15 +52,14 @@ public void validate(Object target, Errors errors) {
this.delegate.validate(target, errors);
}

public static ConfigurationPropertiesJsr303Validator getIfJsr303Present(
ApplicationContext applicationContext) {
public static boolean isJsr303Present(ApplicationContext applicationContext) {
ClassLoader classLoader = applicationContext.getClassLoader();
for (String validatorClass : VALIDATOR_CLASSES) {
if (!ClassUtils.isPresent(validatorClass, classLoader)) {
return null;
return false;
}
}
return new ConfigurationPropertiesJsr303Validator(applicationContext);
return true;
}

private static class Delegate extends LocalValidatorFactoryBean {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* 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");
* you may not use this file except in compliance with the License.
Expand All @@ -25,6 +25,7 @@
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.validation.annotation.Validated;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
Expand All @@ -39,7 +40,7 @@
public class ValidationExceptionFailureAnalyzerTests {

@Test
public void test() {
public void validatedPropertiesTest() {
try {
new AnnotationConfigApplicationContext(TestConfiguration.class).close();
fail("Expected failure did not occur");
Expand All @@ -51,6 +52,12 @@ public void test() {
}
}

@Test
public void nonValidatedPropertiesTest() {
new AnnotationConfigApplicationContext(NonValidatedTestConfiguration.class)
.close();
}

@EnableConfigurationProperties(TestProperties.class)
static class TestConfiguration {

Expand All @@ -60,8 +67,22 @@ static class TestConfiguration {
}

@ConfigurationProperties("test")
@Validated
private static class TestProperties {

}

@EnableConfigurationProperties(NonValidatedTestProperties.class)
static class NonValidatedTestConfiguration {

NonValidatedTestConfiguration(NonValidatedTestProperties testProperties) {
}

}

@ConfigurationProperties("test")
private static class NonValidatedTestProperties {

}

}

0 comments on commit 7bcb5c4

Please sign in to comment.