Merge pull request #14967 from chang-chao

* pr/14967:
  Polish "Consider aliases when checking descendants"
  Consider aliases when checking descendants
pull/16246/head
Phillip Webb 6 years ago
commit 236bd856f6

@ -65,6 +65,15 @@ class AliasedConfigurationPropertySource implements ConfigurationPropertySource
return aliasResult; return aliasResult;
} }
} }
for (ConfigurationPropertyName from : getAliases()) {
for (ConfigurationPropertyName alias : getAliases().getAliases(from)) {
if (name.isAncestorOf(alias)) {
if (this.source.getConfigurationProperty(from) != null) {
return ConfigurationPropertyState.PRESENT;
}
}
}
}
return ConfigurationPropertyState.ABSENT; return ConfigurationPropertyState.ABSENT;
} }

@ -18,6 +18,7 @@ package org.springframework.boot.context.properties.source;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -33,7 +34,8 @@ import org.springframework.util.MultiValueMap;
* @since 2.0.0 * @since 2.0.0
* @see ConfigurationPropertySource#withAliases(ConfigurationPropertyNameAliases) * @see ConfigurationPropertySource#withAliases(ConfigurationPropertyNameAliases)
*/ */
public final class ConfigurationPropertyNameAliases { public final class ConfigurationPropertyNameAliases
implements Iterable<ConfigurationPropertyName> {
private final MultiValueMap<ConfigurationPropertyName, ConfigurationPropertyName> aliases = new LinkedMultiValueMap<>(); private final MultiValueMap<ConfigurationPropertyName, ConfigurationPropertyName> aliases = new LinkedMultiValueMap<>();
@ -74,4 +76,9 @@ public final class ConfigurationPropertyNameAliases {
.findFirst().orElse(null); .findFirst().orElse(null);
} }
@Override
public Iterator<ConfigurationPropertyName> iterator() {
return this.aliases.keySet().iterator();
}
} }

@ -16,6 +16,8 @@
package org.springframework.boot.context.properties.source; package org.springframework.boot.context.properties.source;
import java.util.Collections;
import org.junit.Test; import org.junit.Test;
import org.mockito.Answers; import org.mockito.Answers;
@ -111,6 +113,16 @@ public class AliasedConfigurationPropertySourceTests {
.isEqualTo(ConfigurationPropertyState.PRESENT); .isEqualTo(ConfigurationPropertyState.PRESENT);
} }
@Test
public void containsDescendantOfWhenPresentInAliasShouldReturnPresent() {
ConfigurationPropertySource source = new MapConfigurationPropertySource(
Collections.singletonMap("foo.bar", "foobar"));
ConfigurationPropertySource aliased = source
.withAliases(new ConfigurationPropertyNameAliases("foo.bar", "baz.foo"));
assertThat(aliased.containsDescendantOf(ConfigurationPropertyName.of("baz")))
.isEqualTo(ConfigurationPropertyState.PRESENT);
}
private Object getValue(ConfigurationPropertySource source, String name) { private Object getValue(ConfigurationPropertySource source, String name) {
ConfigurationProperty property = source ConfigurationProperty property = source
.getConfigurationProperty(ConfigurationPropertyName.of(name)); .getConfigurationProperty(ConfigurationPropertyName.of(name));

Loading…
Cancel
Save