Skip to content

Commit

Permalink
Merge pull request #25445 from dreis2211
Browse files Browse the repository at this point in the history
* gh-25445:
  Ignore empty prefixes in new PrefixedConfigurationPropertySource

Closes gh-25445
  • Loading branch information
wilkinsona committed Feb 26, 2021
2 parents a8592f3 + 4b69456 commit 33b3753
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
Expand Up @@ -47,7 +47,10 @@ public ConfigurationProperty getConfigurationProperty(ConfigurationPropertyName
}

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);
}

Expand Down
Expand Up @@ -18,6 +18,8 @@

import java.util.stream.Stream;

import org.springframework.util.StringUtils;

/**
* An iterable {@link PrefixedConfigurationPropertySource}.
*
Expand All @@ -32,6 +34,9 @@ class PrefixedIterableConfigurationPropertySource extends PrefixedConfigurationP

@Override
public Stream<ConfigurationPropertyName> stream() {
if (!StringUtils.hasText(getPrefix())) {
return getSource().stream();
}
ConfigurationPropertyName prefix = ConfigurationPropertyName.of(getPrefix());
return getSource().stream().map((propertyName) -> {
if (prefix.isAncestorOf(propertyName)) {
Expand Down
Expand Up @@ -38,4 +38,15 @@ void streamShouldConsiderPrefix() {
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"));
}

}

0 comments on commit 33b3753

Please sign in to comment.