From 6d9692ffb74051174b60e1d04b9f053f4add863e Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Mon, 12 Mar 2018 12:35:08 -0700 Subject: [PATCH] Fix binding to empty prefix when empty name present Fixes gh-12381 --- .../boot/context/properties/bind/Binder.java | 4 ++++ .../context/properties/bind/BinderTests.java | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java index 19d9c4734a..222940dea5 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java @@ -46,6 +46,7 @@ import org.springframework.core.env.Environment; import org.springframework.format.support.DefaultFormattingConversionService; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.StringUtils; /** * A container object which Binds objects from one or more @@ -308,6 +309,9 @@ public class Binder { private ConfigurationProperty findProperty(ConfigurationPropertyName name, Context context) { + if (!StringUtils.hasText(name.toString())) { + return null; + } return context.streamSources() .map((source) -> source.getConfigurationProperty(name)) .filter(Objects::nonNull).findFirst().orElse(null); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java index 8900be87fd..f7cfe9af49 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java @@ -20,7 +20,10 @@ import java.beans.PropertyEditorSupport; import java.time.LocalDate; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Set; import javax.validation.Validation; @@ -40,6 +43,7 @@ import org.springframework.boot.context.properties.source.ConfigurationPropertyS import org.springframework.boot.context.properties.source.MockConfigurationPropertySource; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.convert.ConversionFailedException; +import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.PropertySource; import org.springframework.core.env.StandardEnvironment; import org.springframework.core.io.Resource; @@ -301,6 +305,20 @@ public class BinderTests { this.binder.bind("foo", target); } + @Test + @SuppressWarnings("unchecked") + public void bindWithEmptyPrefixShouldIgnorePropertiesWithEmptyName() { + Map source = new HashMap<>(); + source.put("value", "hello"); + source.put("", "bar"); + Iterable propertySources = ConfigurationPropertySources.from( + new MapPropertySource("test", source)); + this.sources.addAll((Set) propertySources); + Bindable target = Bindable.of(JavaBean.class); + JavaBean result = this.binder.bind("", target).get(); + assertThat(result.getValue()).isEqualTo("hello"); + } + public static class JavaBean { private String value;