Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.boot.context.properties.source;

import java.util.Set;

import org.springframework.util.Assert;

/**
Expand Down Expand Up @@ -58,11 +60,17 @@ public ConfigurationPropertyState containsDescendantOf(
if (result != ConfigurationPropertyState.ABSENT) {
return result;
}
for (ConfigurationPropertyName alias : getAliases().getAliases(name)) {
ConfigurationPropertyState aliasResult = this.source
.containsDescendantOf(alias);
if (aliasResult != ConfigurationPropertyState.ABSENT) {
return aliasResult;
Set<ConfigurationPropertyName> aliasNames = this.aliases.getAllNames();
for (ConfigurationPropertyName configurationPropertyName : aliasNames) {
boolean descendantPresentInAlias = this.aliases
.getAliases(configurationPropertyName).stream()
.filter(name::isAncestorOf).findFirst().isPresent();
if (descendantPresentInAlias) {
ConfigurationProperty configurationProperty = this.getSource()
.getConfigurationProperty(configurationPropertyName);
if (configurationProperty != null) {
return ConfigurationPropertyState.PRESENT;
}
}
}
return ConfigurationPropertyState.ABSENT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
Expand Down Expand Up @@ -74,4 +75,8 @@ public ConfigurationPropertyName getNameForAlias(ConfigurationPropertyName alias
.findFirst().orElse(null);
}

public Set<ConfigurationPropertyName> getAllNames() {
return this.aliases.keySet();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.junit.Test;
import org.mockito.Answers;

import org.springframework.boot.origin.Origin;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -105,8 +107,30 @@ public void containsDescendantOfWhenAnyIsPresentShouldReturnPresent() {
.willReturn(ConfigurationPropertyState.ABSENT);
given(source.containsDescendantOf(ConfigurationPropertyName.of("bar")))
.willReturn(ConfigurationPropertyState.PRESENT);
ConfigurationPropertyName barBar = ConfigurationPropertyName.of("bar.bar");
given(source.getConfigurationProperty(barBar)).willReturn(
new ConfigurationProperty(barBar, "barBarValue", mock(Origin.class)));
ConfigurationPropertySource aliased = source
.withAliases(new ConfigurationPropertyNameAliases("foo", "bar"));
.withAliases(new ConfigurationPropertyNameAliases("bar.bar", "foo.foo"));
assertThat(aliased.containsDescendantOf(name))
.isEqualTo(ConfigurationPropertyState.PRESENT);
}

@Test
public void containsDescendantOfWhenPresentInAliasShouldReturnPresent() {
ConfigurationPropertyName name = ConfigurationPropertyName.of("baz");
ConfigurationPropertySource source = mock(ConfigurationPropertySource.class,
withSettings().defaultAnswer(Answers.CALLS_REAL_METHODS));
given(source.containsDescendantOf(name))
.willReturn(ConfigurationPropertyState.ABSENT);

ConfigurationPropertyName barFoo = ConfigurationPropertyName.of("bar.foo");

given(source.getConfigurationProperty(barFoo)).willReturn(
new ConfigurationProperty(barFoo, "barFooValue", mock(Origin.class)));

ConfigurationPropertySource aliased = source
.withAliases(new ConfigurationPropertyNameAliases("bar.foo", "baz.foo"));
assertThat(aliased.containsDescendantOf(name))
.isEqualTo(ConfigurationPropertyState.PRESENT);
}
Expand Down