Merge pull request #25445 from dreis2211

* gh-25445:
  Ignore empty prefixes in new PrefixedConfigurationPropertySource

Closes gh-25445
pull/25446/head
Andy Wilkinson 4 years ago
commit 33b3753077

@ -47,7 +47,10 @@ class PrefixedConfigurationPropertySource implements ConfigurationPropertySource
} }
private ConfigurationPropertyName getPrefixedName(ConfigurationPropertyName name) { 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); return ConfigurationPropertyName.of(prefix + name);
} }

@ -18,6 +18,8 @@ package org.springframework.boot.context.properties.source;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.springframework.util.StringUtils;
/** /**
* An iterable {@link PrefixedConfigurationPropertySource}. * An iterable {@link PrefixedConfigurationPropertySource}.
* *
@ -32,6 +34,9 @@ class PrefixedIterableConfigurationPropertySource extends PrefixedConfigurationP
@Override @Override
public Stream<ConfigurationPropertyName> stream() { public Stream<ConfigurationPropertyName> stream() {
if (!StringUtils.hasText(getPrefix())) {
return getSource().stream();
}
ConfigurationPropertyName prefix = ConfigurationPropertyName.of(getPrefix()); ConfigurationPropertyName prefix = ConfigurationPropertyName.of(getPrefix());
return getSource().stream().map((propertyName) -> { return getSource().stream().map((propertyName) -> {
if (prefix.isAncestorOf(propertyName)) { if (prefix.isAncestorOf(propertyName)) {

@ -38,4 +38,15 @@ class PrefixedIterableConfigurationPropertySourceTests {
ConfigurationPropertyName.of("foo.baz"), ConfigurationPropertyName.of("hello.bing")); 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"));
}
} }

Loading…
Cancel
Save