From 0286128b2897131fa3fa2a4e03a624c2b2c2f6ec Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 23 Jan 2017 13:29:26 +0100 Subject: [PATCH] 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 a93eac0e65f..6cf5cbdbaa5 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 683c3ad8d3e..2e06494df43 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 a75bbb58ae9..0ac9ce4f241 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 b5da56ca2d5..ed7a8498f4f 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 5dd52c46cc4..14d8129788f 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