Skip to content

Commit 5603d61

Browse files
committed
Polish "Consider aliases when checking descendants"
See gh-14967
1 parent 256ca68 commit 5603d61

File tree

3 files changed

+26
-35
lines changed

3 files changed

+26
-35
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/AliasedConfigurationPropertySource.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

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

19-
import java.util.Set;
20-
2119
import org.springframework.util.Assert;
2220

2321
/**
@@ -60,16 +58,19 @@ public ConfigurationPropertyState containsDescendantOf(
6058
if (result != ConfigurationPropertyState.ABSENT) {
6159
return result;
6260
}
63-
Set<ConfigurationPropertyName> aliasNames = this.aliases.getAllNames();
64-
for (ConfigurationPropertyName configurationPropertyName : aliasNames) {
65-
boolean descendantPresentInAlias = this.aliases
66-
.getAliases(configurationPropertyName).stream()
67-
.filter(name::isAncestorOf).findFirst().isPresent();
68-
if (descendantPresentInAlias) {
69-
ConfigurationProperty configurationProperty = this.getSource()
70-
.getConfigurationProperty(configurationPropertyName);
71-
if (configurationProperty != null) {
72-
return ConfigurationPropertyState.PRESENT;
61+
for (ConfigurationPropertyName alias : getAliases().getAliases(name)) {
62+
ConfigurationPropertyState aliasResult = this.source
63+
.containsDescendantOf(alias);
64+
if (aliasResult != ConfigurationPropertyState.ABSENT) {
65+
return aliasResult;
66+
}
67+
}
68+
for (ConfigurationPropertyName from : getAliases()) {
69+
for (ConfigurationPropertyName alias : getAliases().getAliases(from)) {
70+
if (name.isAncestorOf(alias)) {
71+
if (this.source.getConfigurationProperty(from) != null) {
72+
return ConfigurationPropertyState.PRESENT;
73+
}
7374
}
7475
}
7576
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyNameAliases.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
import java.util.Arrays;
2020
import java.util.Collections;
21+
import java.util.Iterator;
2122
import java.util.List;
2223
import java.util.Map;
23-
import java.util.Set;
2424

2525
import org.springframework.util.Assert;
2626
import org.springframework.util.LinkedMultiValueMap;
@@ -34,7 +34,8 @@
3434
* @since 2.0.0
3535
* @see ConfigurationPropertySource#withAliases(ConfigurationPropertyNameAliases)
3636
*/
37-
public final class ConfigurationPropertyNameAliases {
37+
public final class ConfigurationPropertyNameAliases
38+
implements Iterable<ConfigurationPropertyName> {
3839

3940
private final MultiValueMap<ConfigurationPropertyName, ConfigurationPropertyName> aliases = new LinkedMultiValueMap<>();
4041

@@ -75,8 +76,9 @@ public ConfigurationPropertyName getNameForAlias(ConfigurationPropertyName alias
7576
.findFirst().orElse(null);
7677
}
7778

78-
public Set<ConfigurationPropertyName> getAllNames() {
79-
return this.aliases.keySet();
79+
@Override
80+
public Iterator<ConfigurationPropertyName> iterator() {
81+
return this.aliases.keySet().iterator();
8082
}
8183

8284
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/AliasedConfigurationPropertySourceTests.java

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

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

19+
import java.util.Collections;
20+
1921
import org.junit.Test;
2022
import org.mockito.Answers;
2123

22-
import org.springframework.boot.origin.Origin;
23-
2424
import static org.assertj.core.api.Assertions.assertThat;
2525
import static org.mockito.BDDMockito.given;
2626
import static org.mockito.Mockito.mock;
@@ -107,31 +107,19 @@ public void containsDescendantOfWhenAnyIsPresentShouldReturnPresent() {
107107
.willReturn(ConfigurationPropertyState.ABSENT);
108108
given(source.containsDescendantOf(ConfigurationPropertyName.of("bar")))
109109
.willReturn(ConfigurationPropertyState.PRESENT);
110-
ConfigurationPropertyName barBar = ConfigurationPropertyName.of("bar.bar");
111-
given(source.getConfigurationProperty(barBar)).willReturn(
112-
new ConfigurationProperty(barBar, "barBarValue", mock(Origin.class)));
113110
ConfigurationPropertySource aliased = source
114-
.withAliases(new ConfigurationPropertyNameAliases("bar.bar", "foo.foo"));
111+
.withAliases(new ConfigurationPropertyNameAliases("foo", "bar"));
115112
assertThat(aliased.containsDescendantOf(name))
116113
.isEqualTo(ConfigurationPropertyState.PRESENT);
117114
}
118115

119116
@Test
120117
public void containsDescendantOfWhenPresentInAliasShouldReturnPresent() {
121-
ConfigurationPropertyName name = ConfigurationPropertyName.of("baz");
122-
ConfigurationPropertySource source = mock(ConfigurationPropertySource.class,
123-
withSettings().defaultAnswer(Answers.CALLS_REAL_METHODS));
124-
given(source.containsDescendantOf(name))
125-
.willReturn(ConfigurationPropertyState.ABSENT);
126-
127-
ConfigurationPropertyName barFoo = ConfigurationPropertyName.of("bar.foo");
128-
129-
given(source.getConfigurationProperty(barFoo)).willReturn(
130-
new ConfigurationProperty(barFoo, "barFooValue", mock(Origin.class)));
131-
118+
ConfigurationPropertySource source = new MapConfigurationPropertySource(
119+
Collections.singletonMap("foo.bar", "foobar"));
132120
ConfigurationPropertySource aliased = source
133-
.withAliases(new ConfigurationPropertyNameAliases("bar.foo", "baz.foo"));
134-
assertThat(aliased.containsDescendantOf(name))
121+
.withAliases(new ConfigurationPropertyNameAliases("foo.bar", "baz.foo"));
122+
assertThat(aliased.containsDescendantOf(ConfigurationPropertyName.of("baz")))
135123
.isEqualTo(ConfigurationPropertyState.PRESENT);
136124
}
137125

0 commit comments

Comments
 (0)