Ignore empty prefixes in new PrefixedConfigurationPropertySource
See gh-25445
This commit is contained in:
committed by
Andy Wilkinson
parent
a8592f36d4
commit
4b694560eb
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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<ConfigurationPropertyName> stream() {
|
||||
if (!StringUtils.hasText(getPrefix())) {
|
||||
return getSource().stream();
|
||||
}
|
||||
ConfigurationPropertyName prefix = ConfigurationPropertyName.of(getPrefix());
|
||||
return getSource().stream().map((propertyName) -> {
|
||||
if (prefix.isAncestorOf(propertyName)) {
|
||||
|
||||
@@ -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"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user