From 0286128b2897131fa3fa2a4e03a624c2b2c2f6ec Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 23 Jan 2017 13:29:26 +0100 Subject: [PATCH 1/2] Deprecate exceptionIfInvalid This commit deprecates the `exceptionIfInvalid` feature as we have now a way to select the `@ConfigurationProperties` object to validate (i.e. add `@Validated` on them). Closes gh-8027 --- .../boot/bind/PropertiesConfigurationFactory.java | 3 +++ .../boot/context/properties/ConfigurationProperties.java | 3 +++ .../ConfigurationPropertiesBindingPostProcessor.java | 1 + .../boot/bind/PropertiesConfigurationFactoryTests.java | 3 ++- .../context/properties/EnableConfigurationPropertiesTests.java | 3 +++ 5 files changed, 12 insertions(+), 1 deletion(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java b/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java index a93eac0e65..6cf5cbdbaa 100644 --- a/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java @@ -186,7 +186,10 @@ public class PropertiesConfigurationFactory * Set a flag to indicate that an exception should be raised if a Validator is * available and validation fails. * @param exceptionIfInvalid the flag to set + * @deprecated as of 1.5, do not specify a {@link Validator} if validation should not + * occur */ + @Deprecated public void setExceptionIfInvalid(boolean exceptionIfInvalid) { this.exceptionIfInvalid = exceptionIfInvalid; } diff --git a/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationProperties.java b/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationProperties.java index 683c3ad8d3..2e06494df4 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationProperties.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationProperties.java @@ -86,7 +86,10 @@ public @interface ConfigurationProperties { * it is set to false, validation errors will be swallowed. They will be logged, but * not propagated to the caller. * @return the flag value (default true) + * @deprecated as of 1.5 since validation only kicks in when {@code @Validated} is + * present */ + @Deprecated boolean exceptionIfInvalid() default true; } diff --git a/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessor.java b/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessor.java index a75bbb58ae..0ac9ce4f24 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessor.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessor.java @@ -304,6 +304,7 @@ public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProc return bean; } + @SuppressWarnings("deprecation") private void postProcessBeforeInitialization(Object bean, String beanName, ConfigurationProperties annotation) { Object target = bean; diff --git a/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryTests.java b/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryTests.java index b5da56ca2d..ed7a8498f4 100644 --- a/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 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. @@ -84,6 +84,7 @@ public class PropertiesConfigurationFactoryTests { } @Test + @Deprecated public void testValidationErrorCanBeSuppressed() throws Exception { this.validator = new SpringValidatorAdapter( Validation.buildDefaultValidatorFactory().getValidator()); diff --git a/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesTests.java b/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesTests.java index 5dd52c46cc..14d8129788 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesTests.java @@ -185,6 +185,7 @@ public class EnableConfigurationPropertiesTests { } @Test + @Deprecated public void testNoExceptionOnValidation() { this.context.register(NoExceptionIfInvalidTestConfiguration.class); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, @@ -452,6 +453,7 @@ public class EnableConfigurationPropertiesTests { @Configuration @EnableConfigurationProperties(NoExceptionIfInvalidTestProperties.class) + @Deprecated protected static class NoExceptionIfInvalidTestConfiguration { } @@ -711,6 +713,7 @@ public class EnableConfigurationPropertiesTests { @ConfigurationProperties(exceptionIfInvalid = false) @Validated + @Deprecated protected static class NoExceptionIfInvalidTestProperties extends TestProperties { @NotNull From f36b051071e68b0ccd69aed7c8f5bb9255bd86a8 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 23 Jan 2017 13:47:09 +0100 Subject: [PATCH 2/2] Deprecate exceptionIfInvalid for yaml processing See gh-8027 --- .../boot/bind/YamlConfigurationFactory.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/YamlConfigurationFactory.java b/spring-boot/src/main/java/org/springframework/boot/bind/YamlConfigurationFactory.java index fd45de3296..6c14fc2a70 100644 --- a/spring-boot/src/main/java/org/springframework/boot/bind/YamlConfigurationFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/bind/YamlConfigurationFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 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. @@ -122,6 +122,14 @@ public class YamlConfigurationFactory this.validator = validator; } + /** + * Set a flag to indicate that an exception should be raised if a Validator is + * available and validation fails. + * @param exceptionIfInvalid the flag to set + * @deprecated as of 1.5, do not specify a {@link Validator} if validation should not + * occur + */ + @Deprecated public void setExceptionIfInvalid(boolean exceptionIfInvalid) { this.exceptionIfInvalid = exceptionIfInvalid; }