Skip to content

Commit 236bd85

Browse files
committed
Merge pull request #14967 from chang-chao
* pr/14967: Polish "Consider aliases when checking descendants" Consider aliases when checking descendants
2 parents 7bb6df4 + 5603d61 commit 236bd85

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ public ConfigurationPropertyState containsDescendantOf(
6565
return aliasResult;
6666
}
6767
}
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+
}
74+
}
75+
}
76+
}
6877
return ConfigurationPropertyState.ABSENT;
6978
}
7079

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
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;
2324

@@ -33,7 +34,8 @@
3334
* @since 2.0.0
3435
* @see ConfigurationPropertySource#withAliases(ConfigurationPropertyNameAliases)
3536
*/
36-
public final class ConfigurationPropertyNameAliases {
37+
public final class ConfigurationPropertyNameAliases
38+
implements Iterable<ConfigurationPropertyName> {
3739

3840
private final MultiValueMap<ConfigurationPropertyName, ConfigurationPropertyName> aliases = new LinkedMultiValueMap<>();
3941

@@ -74,4 +76,9 @@ public ConfigurationPropertyName getNameForAlias(ConfigurationPropertyName alias
7476
.findFirst().orElse(null);
7577
}
7678

79+
@Override
80+
public Iterator<ConfigurationPropertyName> iterator() {
81+
return this.aliases.keySet().iterator();
82+
}
83+
7784
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
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

@@ -111,6 +113,16 @@ public void containsDescendantOfWhenAnyIsPresentShouldReturnPresent() {
111113
.isEqualTo(ConfigurationPropertyState.PRESENT);
112114
}
113115

116+
@Test
117+
public void containsDescendantOfWhenPresentInAliasShouldReturnPresent() {
118+
ConfigurationPropertySource source = new MapConfigurationPropertySource(
119+
Collections.singletonMap("foo.bar", "foobar"));
120+
ConfigurationPropertySource aliased = source
121+
.withAliases(new ConfigurationPropertyNameAliases("foo.bar", "baz.foo"));
122+
assertThat(aliased.containsDescendantOf(ConfigurationPropertyName.of("baz")))
123+
.isEqualTo(ConfigurationPropertyState.PRESENT);
124+
}
125+
114126
private Object getValue(ConfigurationPropertySource source, String name) {
115127
ConfigurationProperty property = source
116128
.getConfigurationProperty(ConfigurationPropertyName.of(name));

0 commit comments

Comments
 (0)