Skip to content

Commit b926b59

Browse files
committed
Hacking
1 parent 7755e0c commit b926b59

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,17 @@ public Iterator<ConfigurationPropertyName> iterator() {
8686

8787
@Override
8888
public ConfigurationPropertyState containsDescendantOf(ConfigurationPropertyName name) {
89-
return ConfigurationPropertyState.search(this, name::isAncestorOf);
89+
return ConfigurationPropertyState.search(this, (propertyName) -> {
90+
boolean ancestorOf = name.isAncestorOf(propertyName);
91+
if (ancestorOf || !shouldTryLegacyName(name.toString())) {
92+
return ancestorOf;
93+
}
94+
return ConfigurationPropertyName.of(name.toString().replace('-', '.')).isAncestorOf(propertyName);
95+
});
96+
}
97+
98+
private boolean shouldTryLegacyName(String name) {
99+
return this.getPropertySource() instanceof SystemEnvironmentPropertySource && name.contains("-");
90100
}
91101

92102
private List<ConfigurationPropertyName> getConfigurationPropertyNames() {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.springframework.core.env.EnumerablePropertySource;
3232
import org.springframework.core.env.MapPropertySource;
3333
import org.springframework.core.env.PropertySource;
34+
import org.springframework.core.env.StandardEnvironment;
35+
import org.springframework.core.env.SystemEnvironmentPropertySource;
3436

3537
import static org.assertj.core.api.Assertions.assertThat;
3638
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@@ -154,6 +156,23 @@ void containsDescendantOfShouldCheckSourceNames() {
154156
.isEqualTo(ConfigurationPropertyState.ABSENT);
155157
}
156158

159+
@Test
160+
void containsDescendantOfWhenSystemEnvironmentPropertySourceShouldLegacyProperty() {
161+
Map<String, Object> source = new LinkedHashMap<>();
162+
source.put("FOO_BAR_BAZ_BONG", "bing");
163+
source.put("FOO_ALPHABRAVO_GAMMA", "delta");
164+
SystemEnvironmentPropertySource propertySource = new SystemEnvironmentPropertySource(
165+
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, source);
166+
SpringIterableConfigurationPropertySource adapter = new SpringIterableConfigurationPropertySource(
167+
propertySource, SystemEnvironmentPropertyMapper.INSTANCE);
168+
assertThat(adapter.containsDescendantOf(ConfigurationPropertyName.of("foo.bar-baz")))
169+
.isEqualTo(ConfigurationPropertyState.PRESENT);
170+
assertThat(adapter.containsDescendantOf(ConfigurationPropertyName.of("foo.alpha-bravo")))
171+
.isEqualTo(ConfigurationPropertyState.PRESENT);
172+
assertThat(adapter.containsDescendantOf(ConfigurationPropertyName.of("foo.blah")))
173+
.isEqualTo(ConfigurationPropertyState.ABSENT);
174+
}
175+
157176
@Test
158177
void simpleMapPropertySourceKeyDataChangeInvalidatesCache() {
159178
// gh-13344

0 commit comments

Comments
 (0)