Skip to content

Commit 7bcb5c4

Browse files
committed
Merge pull request #12669 from geo-m
* pr/12669: Polish "Allow validation api without implementation" Allow validation api without implementation
2 parents b67e6aa + 0c98d0e commit 7bcb5c4

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBinder.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ class ConfigurationPropertiesBinder {
5555

5656
private final Validator configurationPropertiesValidator;
5757

58-
private final Validator jsr303Validator;
58+
private final boolean jsr303Present;
59+
60+
private volatile Validator jsr303Validator;
5961

6062
private volatile Binder binder;
6163

@@ -66,8 +68,8 @@ class ConfigurationPropertiesBinder {
6668
.getPropertySources();
6769
this.configurationPropertiesValidator = getConfigurationPropertiesValidator(
6870
applicationContext, validatorBeanName);
69-
this.jsr303Validator = ConfigurationPropertiesJsr303Validator
70-
.getIfJsr303Present(applicationContext);
71+
this.jsr303Present = ConfigurationPropertiesJsr303Validator
72+
.isJsr303Present(applicationContext);
7173
}
7274

7375
public void bind(Bindable<?> target) {
@@ -93,16 +95,23 @@ private List<Validator> getValidators(Bindable<?> target) {
9395
if (this.configurationPropertiesValidator != null) {
9496
validators.add(this.configurationPropertiesValidator);
9597
}
96-
if (this.jsr303Validator != null
97-
&& target.getAnnotation(Validated.class) != null) {
98-
validators.add(this.jsr303Validator);
98+
if (this.jsr303Present && target.getAnnotation(Validated.class) != null) {
99+
validators.add(getJsr303Validator());
99100
}
100101
if (target.getValue() != null && target.getValue().get() instanceof Validator) {
101102
validators.add((Validator) target.getValue().get());
102103
}
103104
return validators;
104105
}
105106

107+
private Validator getJsr303Validator() {
108+
if (this.jsr303Validator == null) {
109+
this.jsr303Validator = new ConfigurationPropertiesJsr303Validator(
110+
this.applicationContext);
111+
}
112+
return this.jsr303Validator;
113+
}
114+
106115
private BindHandler getBindHandler(ConfigurationProperties annotation,
107116
List<Validator> validators) {
108117
BindHandler handler = new IgnoreTopLevelConverterNotFoundBindHandler();

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesJsr303Validator.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ final class ConfigurationPropertiesJsr303Validator implements Validator {
3838

3939
private final Delegate delegate;
4040

41-
private ConfigurationPropertiesJsr303Validator(
42-
ApplicationContext applicationContext) {
41+
ConfigurationPropertiesJsr303Validator(ApplicationContext applicationContext) {
4342
this.delegate = new Delegate(applicationContext);
4443
}
4544

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

56-
public static ConfigurationPropertiesJsr303Validator getIfJsr303Present(
57-
ApplicationContext applicationContext) {
55+
public static boolean isJsr303Present(ApplicationContext applicationContext) {
5856
ClassLoader classLoader = applicationContext.getClassLoader();
5957
for (String validatorClass : VALIDATOR_CLASSES) {
6058
if (!ClassUtils.isPresent(validatorClass, classLoader)) {
61-
return null;
59+
return false;
6260
}
6361
}
64-
return new ConfigurationPropertiesJsr303Validator(applicationContext);
62+
return true;
6563
}
6664

6765
private static class Delegate extends LocalValidatorFactoryBean {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/ValidationExceptionFailureAnalyzerTests.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
2626
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
2727
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
28+
import org.springframework.validation.annotation.Validated;
2829

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

4142
@Test
42-
public void test() {
43+
public void validatedPropertiesTest() {
4344
try {
4445
new AnnotationConfigApplicationContext(TestConfiguration.class).close();
4546
fail("Expected failure did not occur");
@@ -51,6 +52,12 @@ public void test() {
5152
}
5253
}
5354

55+
@Test
56+
public void nonValidatedPropertiesTest() {
57+
new AnnotationConfigApplicationContext(NonValidatedTestConfiguration.class)
58+
.close();
59+
}
60+
5461
@EnableConfigurationProperties(TestProperties.class)
5562
static class TestConfiguration {
5663

@@ -60,8 +67,22 @@ static class TestConfiguration {
6067
}
6168

6269
@ConfigurationProperties("test")
70+
@Validated
6371
private static class TestProperties {
6472

6573
}
6674

75+
@EnableConfigurationProperties(NonValidatedTestProperties.class)
76+
static class NonValidatedTestConfiguration {
77+
78+
NonValidatedTestConfiguration(NonValidatedTestProperties testProperties) {
79+
}
80+
81+
}
82+
83+
@ConfigurationProperties("test")
84+
private static class NonValidatedTestProperties {
85+
86+
}
87+
6788
}

0 commit comments

Comments
 (0)