Added exceptionIfInvalid attribute to @ConfigurationProperties
Default behaviour unchanged, but if you want to ignore validation errors you can switch them off for indivdual beans now. Fixes gh-144
This commit is contained in:
@@ -173,6 +173,12 @@ public class PropertiesConfigurationFactory<T> implements FactoryBean<T>,
|
||||
this.validator = validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flag to indicate that an exception should be raised if a Validator is available and
|
||||
* validation fails.
|
||||
*
|
||||
* @param exceptionIfInvalid the flag to set
|
||||
*/
|
||||
public void setExceptionIfInvalid(boolean exceptionIfInvalid) {
|
||||
this.exceptionIfInvalid = exceptionIfInvalid;
|
||||
}
|
||||
|
||||
@@ -72,6 +72,13 @@ public @interface ConfigurationProperties {
|
||||
*/
|
||||
boolean ignoreUnknownFields() default true;
|
||||
|
||||
/**
|
||||
* Flag to indicate that validation errors can be swallowed. If set they will be
|
||||
* logged, but not propagate to the caller.
|
||||
* @return the flag value (default true)
|
||||
*/
|
||||
boolean exceptionIfInvalid() default true;
|
||||
|
||||
/**
|
||||
* Optionally provide an explicit resource path to bind to instead of using the
|
||||
* default environment.
|
||||
|
||||
@@ -302,6 +302,7 @@ public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProc
|
||||
if (annotation != null) {
|
||||
factory.setIgnoreInvalidFields(annotation.ignoreInvalidFields());
|
||||
factory.setIgnoreUnknownFields(annotation.ignoreUnknownFields());
|
||||
factory.setExceptionIfInvalid(annotation.exceptionIfInvalid());
|
||||
factory.setIgnoreNestedProperties(annotation.ignoreNestedProperties());
|
||||
String targetName = (StringUtils.hasLength(annotation.value()) ? annotation
|
||||
.value() : annotation.name());
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.boot.bind;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@@ -85,6 +87,15 @@ public class PropertiesConfigurationFactoryTests {
|
||||
createFoo("bar: blah");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidationErrorCanBeSuppressed() throws Exception {
|
||||
this.validator = new SpringValidatorAdapter(Validation
|
||||
.buildDefaultValidatorFactory().getValidator());
|
||||
setupFactory();
|
||||
this.factory.setExceptionIfInvalid(false);
|
||||
bindFoo("bar: blah");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindToNamedTarget() throws Exception {
|
||||
this.targetName = "foo";
|
||||
@@ -93,15 +104,23 @@ public class PropertiesConfigurationFactoryTests {
|
||||
}
|
||||
|
||||
private Foo createFoo(final String values) throws Exception {
|
||||
this.factory = new PropertiesConfigurationFactory<Foo>(Foo.class);
|
||||
setupFactory();
|
||||
return bindFoo(values);
|
||||
}
|
||||
|
||||
private Foo bindFoo(final String values) throws Exception {
|
||||
this.factory.setProperties(PropertiesLoaderUtils
|
||||
.loadProperties(new ByteArrayResource(values.getBytes())));
|
||||
this.factory.afterPropertiesSet();
|
||||
return this.factory.getObject();
|
||||
}
|
||||
|
||||
private void setupFactory() throws IOException {
|
||||
this.factory = new PropertiesConfigurationFactory<Foo>(Foo.class);
|
||||
this.factory.setValidator(this.validator);
|
||||
this.factory.setTargetName(this.targetName);
|
||||
this.factory.setIgnoreUnknownFields(this.ignoreUnknownFields);
|
||||
this.factory.setMessageSource(new StaticMessageSource());
|
||||
this.factory.afterPropertiesSet();
|
||||
return this.factory.getObject();
|
||||
}
|
||||
|
||||
// Foo needs to be public and to have setters for all properties
|
||||
|
||||
@@ -21,9 +21,13 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.After;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.TestUtils;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
@@ -31,6 +35,7 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.validation.BindException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
@@ -44,6 +49,9 @@ public class EnableConfigurationPropertiesTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
|
||||
@Rule
|
||||
public ExpectedException expected = ExpectedException.none();
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
System.clearProperty("name");
|
||||
@@ -111,6 +119,26 @@ public class EnableConfigurationPropertiesTests {
|
||||
assertEquals("foo", this.context.getBean(TestProperties.class).name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExceptionOnValidation() {
|
||||
this.context.register(ExceptionIfInvalidTestConfiguration.class);
|
||||
TestUtils.addEnviroment(this.context, "name:foo");
|
||||
this.expected.expectCause(Matchers.<Throwable> instanceOf(BindException.class));
|
||||
this.context.refresh();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoExceptionOnValidation() {
|
||||
this.context.register(NoExceptionIfInvalidTestConfiguration.class);
|
||||
TestUtils.addEnviroment(this.context, "name:foo");
|
||||
this.context.refresh();
|
||||
assertEquals(
|
||||
1,
|
||||
this.context
|
||||
.getBeanNamesForType(NoExceptionIfInvalidTestProperties.class).length);
|
||||
assertEquals("foo", this.context.getBean(TestProperties.class).name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNestedPropertiesBinding() {
|
||||
this.context.register(NestedConfiguration.class);
|
||||
@@ -264,6 +292,16 @@ public class EnableConfigurationPropertiesTests {
|
||||
protected static class IgnoreNestedTestConfiguration {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(ExceptionIfInvalidTestProperties.class)
|
||||
protected static class ExceptionIfInvalidTestConfiguration {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(NoExceptionIfInvalidTestProperties.class)
|
||||
protected static class NoExceptionIfInvalidTestConfiguration {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(DerivedProperties.class)
|
||||
protected static class DerivedConfiguration {
|
||||
@@ -379,6 +417,38 @@ public class EnableConfigurationPropertiesTests {
|
||||
|
||||
}
|
||||
|
||||
@ConfigurationProperties
|
||||
protected static class ExceptionIfInvalidTestProperties extends TestProperties {
|
||||
|
||||
@NotNull
|
||||
private String description;
|
||||
|
||||
public String getDescription() {
|
||||
return this.description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ConfigurationProperties(exceptionIfInvalid = false)
|
||||
protected static class NoExceptionIfInvalidTestProperties extends TestProperties {
|
||||
|
||||
@NotNull
|
||||
private String description;
|
||||
|
||||
public String getDescription() {
|
||||
return this.description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected static class MoreProperties {
|
||||
private String name;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user