Support lower case configuration properties system environment names

Update `SystemEnvironmentPropertyMapper` to generate both mappings
in both the original case and upper case.

This restores the behavior of Spring Boot 3.4 where the system
environment could contain lowercase names.

Fixes gh-45741
This commit is contained in:
Phillip Webb
2025-06-09 17:51:17 +01:00
parent 2f891c250b
commit bb13eaa837
3 changed files with 13 additions and 16 deletions

View File

@@ -543,25 +543,24 @@ public final class ConfigurationPropertyName implements Comparable<Configuration
@Override
public String toString() {
return toString(ToStringFormat.DEFAULT);
return toString(ToStringFormat.DEFAULT, false);
}
String toString(ToStringFormat format) {
String toString(ToStringFormat format, boolean upperCase) {
String string = this.string[format.ordinal()];
if (string == null) {
string = buildToString(format);
this.string[format.ordinal()] = string;
}
return string;
return (!upperCase) ? string : string.toUpperCase(Locale.ENGLISH);
}
private String buildToString(ToStringFormat format) {
return switch (format) {
case DEFAULT -> buildDefaultToString();
case SYSTEM_ENVIRONMENT ->
buildSimpleToString('_', (i) -> getElement(i, Form.UNIFORM).toUpperCase(Locale.ENGLISH));
case LEGACY_SYSTEM_ENVIRONMENT -> buildSimpleToString('_',
(i) -> getElement(i, Form.ORIGINAL).replace('-', '_').toUpperCase(Locale.ENGLISH));
case SYSTEM_ENVIRONMENT -> buildSimpleToString('_', (i) -> getElement(i, Form.UNIFORM));
case LEGACY_SYSTEM_ENVIRONMENT ->
buildSimpleToString('_', (i) -> getElement(i, Form.ORIGINAL).replace('-', '_'));
};
}

View File

@@ -16,6 +16,7 @@
package org.springframework.boot.context.properties.source;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
@@ -109,7 +110,8 @@ class SpringConfigurationPropertySource implements ConfigurationPropertySource {
}
Object getSystemEnvironmentProperty(Map<String, Object> systemEnvironment, String name) {
return systemEnvironment.get(name);
Object value = systemEnvironment.get(name);
return (value != null) ? value : systemEnvironment.get(name.toLowerCase(Locale.ROOT));
}
@Override

View File

@@ -16,8 +16,6 @@
package org.springframework.boot.context.properties.source;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.function.BiPredicate;
@@ -42,12 +40,10 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper {
@Override
public List<String> map(ConfigurationPropertyName configurationPropertyName) {
String name = configurationPropertyName.toString(ToStringFormat.SYSTEM_ENVIRONMENT);
String legacyName = configurationPropertyName.toString(ToStringFormat.LEGACY_SYSTEM_ENVIRONMENT);
if (name.equals(legacyName)) {
return Collections.singletonList(name);
}
return Arrays.asList(name, legacyName);
return List.of(configurationPropertyName.toString(ToStringFormat.SYSTEM_ENVIRONMENT, true),
configurationPropertyName.toString(ToStringFormat.LEGACY_SYSTEM_ENVIRONMENT, true),
configurationPropertyName.toString(ToStringFormat.SYSTEM_ENVIRONMENT, false),
configurationPropertyName.toString(ToStringFormat.LEGACY_SYSTEM_ENVIRONMENT, false));
}
@Override