Skip to content

Commit 06f763a

Browse files
committed
Merge branch 'main' into 4.0.x
2 parents c2cf609 + bb13eaa commit 06f763a

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -543,25 +543,24 @@ ConfigurationPropertyName asSystemEnvironmentLegacyName() {
543543

544544
@Override
545545
public String toString() {
546-
return toString(ToStringFormat.DEFAULT);
546+
return toString(ToStringFormat.DEFAULT, false);
547547
}
548548

549-
String toString(ToStringFormat format) {
549+
String toString(ToStringFormat format, boolean upperCase) {
550550
String string = this.string[format.ordinal()];
551551
if (string == null) {
552552
string = buildToString(format);
553553
this.string[format.ordinal()] = string;
554554
}
555-
return string;
555+
return (!upperCase) ? string : string.toUpperCase(Locale.ENGLISH);
556556
}
557557

558558
private String buildToString(ToStringFormat format) {
559559
return switch (format) {
560560
case DEFAULT -> buildDefaultToString();
561-
case SYSTEM_ENVIRONMENT ->
562-
buildSimpleToString('_', (i) -> getElement(i, Form.UNIFORM).toUpperCase(Locale.ENGLISH));
563-
case LEGACY_SYSTEM_ENVIRONMENT -> buildSimpleToString('_',
564-
(i) -> getElement(i, Form.ORIGINAL).replace('-', '_').toUpperCase(Locale.ENGLISH));
561+
case SYSTEM_ENVIRONMENT -> buildSimpleToString('_', (i) -> getElement(i, Form.UNIFORM));
562+
case LEGACY_SYSTEM_ENVIRONMENT ->
563+
buildSimpleToString('_', (i) -> getElement(i, Form.ORIGINAL).replace('-', '_'));
565564
};
566565
}
567566

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

19+
import java.util.Locale;
1920
import java.util.Map;
2021
import java.util.Random;
2122

@@ -109,7 +110,8 @@ protected final Object getPropertySourceProperty(String name) {
109110
}
110111

111112
Object getSystemEnvironmentProperty(Map<String, Object> systemEnvironment, String name) {
112-
return systemEnvironment.get(name);
113+
Object value = systemEnvironment.get(name);
114+
return (value != null) ? value : systemEnvironment.get(name.toLowerCase(Locale.ROOT));
113115
}
114116

115117
@Override

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

Lines changed: 4 additions & 8 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.Arrays;
20-
import java.util.Collections;
2119
import java.util.List;
2220
import java.util.Locale;
2321
import java.util.function.BiPredicate;
@@ -42,12 +40,10 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper {
4240

4341
@Override
4442
public List<String> map(ConfigurationPropertyName configurationPropertyName) {
45-
String name = configurationPropertyName.toString(ToStringFormat.SYSTEM_ENVIRONMENT);
46-
String legacyName = configurationPropertyName.toString(ToStringFormat.LEGACY_SYSTEM_ENVIRONMENT);
47-
if (name.equals(legacyName)) {
48-
return Collections.singletonList(name);
49-
}
50-
return Arrays.asList(name, legacyName);
43+
return List.of(configurationPropertyName.toString(ToStringFormat.SYSTEM_ENVIRONMENT, true),
44+
configurationPropertyName.toString(ToStringFormat.LEGACY_SYSTEM_ENVIRONMENT, true),
45+
configurationPropertyName.toString(ToStringFormat.SYSTEM_ENVIRONMENT, false),
46+
configurationPropertyName.toString(ToStringFormat.LEGACY_SYSTEM_ENVIRONMENT, false));
5147
}
5248

5349
@Override

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,12 @@ void containsDescendantOfShouldCheckSourceNames() {
152152
}
153153

154154
@Test
155-
void containsDescendantOfWhenSystemEnvironmentPropertySourceShouldLegacyProperty() {
155+
void containsDescendantOfWhenSystemEnvironmentPropertySourceShouldSupportLegacyProperty() {
156156
Map<String, Object> source = new LinkedHashMap<>();
157157
source.put("FOO_BAR_BAZ_BONG", "bing");
158158
source.put("FOO_ALPHABRAVO_GAMMA", "delta");
159+
source.put("loo_bar_baz_bong", "bing");
160+
source.put("loo_alphabravo_gamma", "delta");
159161
SystemEnvironmentPropertySource propertySource = new SystemEnvironmentPropertySource(
160162
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, source);
161163
SpringIterableConfigurationPropertySource adapter = new SpringIterableConfigurationPropertySource(
@@ -166,6 +168,27 @@ void containsDescendantOfWhenSystemEnvironmentPropertySourceShouldLegacyProperty
166168
.isEqualTo(ConfigurationPropertyState.PRESENT);
167169
assertThat(adapter.containsDescendantOf(ConfigurationPropertyName.of("foo.blah")))
168170
.isEqualTo(ConfigurationPropertyState.ABSENT);
171+
assertThat(adapter.containsDescendantOf(ConfigurationPropertyName.of("loo.bar-baz")))
172+
.isEqualTo(ConfigurationPropertyState.PRESENT);
173+
assertThat(adapter.containsDescendantOf(ConfigurationPropertyName.of("loo.alpha-bravo")))
174+
.isEqualTo(ConfigurationPropertyState.PRESENT);
175+
assertThat(adapter.containsDescendantOf(ConfigurationPropertyName.of("loo.blah")))
176+
.isEqualTo(ConfigurationPropertyState.ABSENT);
177+
}
178+
179+
@Test
180+
void getConfigurationPropertyWhenSystemEnvironmentPropertySourceShouldSupportLegacyProperty() {
181+
Map<String, Object> source = new LinkedHashMap<>();
182+
source.put("TEST_VALUE_UPPER", "upper");
183+
source.put("test_value_lower", "lower");
184+
SystemEnvironmentPropertySource propertySource = new SystemEnvironmentPropertySource(
185+
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, source);
186+
SpringIterableConfigurationPropertySource adapter = new SpringIterableConfigurationPropertySource(
187+
propertySource, true, SystemEnvironmentPropertyMapper.INSTANCE, DefaultPropertyMapper.INSTANCE);
188+
assertThat(adapter.getConfigurationProperty(ConfigurationPropertyName.of("test.value-upper")).getValue())
189+
.isEqualTo("upper");
190+
assertThat(adapter.getConfigurationProperty(ConfigurationPropertyName.of("test.value-lower")).getValue())
191+
.isEqualTo("lower");
169192
}
170193

171194
@Test

0 commit comments

Comments
 (0)