From 1b48daf1d5c58325837f9ffdacc7302f5d607d21 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 27 Jul 2020 13:28:44 +0100 Subject: [PATCH] Do not ignore unknown fields when invalid fields are being ignored Previously, ignoring invalid fields would cause the failure for an unknown field to be ignored, irrespective of the ignoreUnknownFields attribute on `@ConfigurationProperties`. This commit updates the NoUnboundElementsBindHandler to ensure that any UnboundConfigurationPropertiesException is thrown rather than being ignored when the handler has been wrapped by an IgnoreErrorsBindHandler. Fixes gh-22308 --- .../handler/NoUnboundElementsBindHandler.java | 9 +++++++++ .../ConfigurationPropertiesTests.java | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/handler/NoUnboundElementsBindHandler.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/handler/NoUnboundElementsBindHandler.java index b0898aef03c..f5c6a488c69 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/handler/NoUnboundElementsBindHandler.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/handler/NoUnboundElementsBindHandler.java @@ -72,6 +72,15 @@ public class NoUnboundElementsBindHandler extends AbstractBindHandler { return super.onSuccess(name, target, context, result); } + @Override + public Object onFailure(ConfigurationPropertyName name, Bindable target, BindContext context, Exception error) + throws Exception { + if (error instanceof UnboundConfigurationPropertiesException) { + throw error; + } + return super.onFailure(name, target, context, error); + } + @Override public void onFinish(ConfigurationPropertyName name, Bindable target, BindContext context, Object result) throws Exception { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java index 3a455d4ec31..3b5b2f3efe3 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java @@ -170,6 +170,14 @@ class ConfigurationPropertiesTests { .withCauseInstanceOf(BindException.class); } + @Test + void givenIgnoreUnknownFieldsFalseAndIgnoreInvalidFieldsTrueWhenThereAreUnknownFieldsThenBindingShouldFail() { + removeSystemProperties(); + assertThatExceptionOfType(ConfigurationPropertiesBindException.class).isThrownBy( + () -> load(IgnoreUnknownFieldsFalseIgnoreInvalidFieldsTrueConfiguration.class, "name=foo", "bar=baz")) + .withCauseInstanceOf(BindException.class); + } + @Test void loadWhenHasIgnoreInvalidFieldsTrueAndInvalidFieldsShouldBind() { load(IgnoreInvalidFieldsFalseProperties.class, "com.example.bar=spam"); @@ -961,6 +969,12 @@ class ConfigurationPropertiesTests { } + @Configuration(proxyBeanMethods = false) + @EnableConfigurationProperties(IgnoreUnknownFieldsFalseIgnoreInvalidFieldsTrueProperties.class) + static class IgnoreUnknownFieldsFalseIgnoreInvalidFieldsTrueConfiguration { + + } + @Configuration(proxyBeanMethods = false) @EnableConfigurationProperties(PrefixProperties.class) static class PrefixConfiguration { @@ -1408,6 +1422,11 @@ class ConfigurationPropertiesTests { } + @ConfigurationProperties(ignoreUnknownFields = false, ignoreInvalidFields = true) + static class IgnoreUnknownFieldsFalseIgnoreInvalidFieldsTrueProperties extends BasicProperties { + + } + @EnableConfigurationProperties @ConfigurationProperties(prefix = "com.example", ignoreInvalidFields = true) static class IgnoreInvalidFieldsFalseProperties {