From 4b694560ebaf2e1724e53b7d684cabc236acdffc Mon Sep 17 00:00:00 2001 From: dreis2211 Date: Fri, 26 Feb 2021 09:50:11 +0100 Subject: [PATCH] Ignore empty prefixes in new PrefixedConfigurationPropertySource See gh-25445 --- .../source/PrefixedConfigurationPropertySource.java | 5 ++++- .../PrefixedIterableConfigurationPropertySource.java | 5 +++++ ...fixedIterableConfigurationPropertySourceTests.java | 11 +++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/PrefixedConfigurationPropertySource.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/PrefixedConfigurationPropertySource.java index e6f5db2db0..9ad40f54ad 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/PrefixedConfigurationPropertySource.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/PrefixedConfigurationPropertySource.java @@ -47,7 +47,10 @@ class PrefixedConfigurationPropertySource implements ConfigurationPropertySource } private ConfigurationPropertyName getPrefixedName(ConfigurationPropertyName name) { - String prefix = (StringUtils.hasText(this.prefix)) ? this.prefix + "." : ""; + if (!StringUtils.hasText(this.prefix)) { + return name; + } + String prefix = this.prefix + "."; return ConfigurationPropertyName.of(prefix + name); } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/PrefixedIterableConfigurationPropertySource.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/PrefixedIterableConfigurationPropertySource.java index dc82761d22..5e4a424437 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/PrefixedIterableConfigurationPropertySource.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/PrefixedIterableConfigurationPropertySource.java @@ -18,6 +18,8 @@ package org.springframework.boot.context.properties.source; import java.util.stream.Stream; +import org.springframework.util.StringUtils; + /** * An iterable {@link PrefixedConfigurationPropertySource}. * @@ -32,6 +34,9 @@ class PrefixedIterableConfigurationPropertySource extends PrefixedConfigurationP @Override public Stream stream() { + if (!StringUtils.hasText(getPrefix())) { + return getSource().stream(); + } ConfigurationPropertyName prefix = ConfigurationPropertyName.of(getPrefix()); return getSource().stream().map((propertyName) -> { if (prefix.isAncestorOf(propertyName)) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/PrefixedIterableConfigurationPropertySourceTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/PrefixedIterableConfigurationPropertySourceTests.java index cf91c994d0..31b3d2438c 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/PrefixedIterableConfigurationPropertySourceTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/PrefixedIterableConfigurationPropertySourceTests.java @@ -38,4 +38,15 @@ class PrefixedIterableConfigurationPropertySourceTests { ConfigurationPropertyName.of("foo.baz"), ConfigurationPropertyName.of("hello.bing")); } + @Test + void emptyPrefixShouldReturnOriginalStream() { + MockConfigurationPropertySource source = new MockConfigurationPropertySource(); + source.put("my.foo.bar", "bing"); + source.put("my.foo.baz", "biff"); + source.put("hello.bing", "blah"); + IterableConfigurationPropertySource prefixed = source.withPrefix(""); + assertThat(prefixed.stream()).containsExactly(ConfigurationPropertyName.of("my.foo.bar"), + ConfigurationPropertyName.of("my.foo.baz"), ConfigurationPropertyName.of("hello.bing")); + } + }