Browse Source

Merge branch 'main' into 4.0.x

pull/45939/head
Phillip Webb 8 months ago
parent
commit
06f763a35b
  1. 13
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java
  2. 4
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySource.java
  3. 12
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java
  4. 25
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringIterableConfigurationPropertySourceTests.java

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

@ -543,25 +543,24 @@ public final class ConfigurationPropertyName implements Comparable<Configuration @@ -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('-', '_'));
};
}

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

@ -16,6 +16,7 @@ @@ -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 { @@ -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

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

@ -16,8 +16,6 @@ @@ -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 { @@ -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

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

@ -152,10 +152,12 @@ class SpringIterableConfigurationPropertySourceTests { @@ -152,10 +152,12 @@ class SpringIterableConfigurationPropertySourceTests {
}
@Test
void containsDescendantOfWhenSystemEnvironmentPropertySourceShouldLegacyProperty() {
void containsDescendantOfWhenSystemEnvironmentPropertySourceShouldSupportLegacyProperty() {
Map<String, Object> source = new LinkedHashMap<>();
source.put("FOO_BAR_BAZ_BONG", "bing");
source.put("FOO_ALPHABRAVO_GAMMA", "delta");
source.put("loo_bar_baz_bong", "bing");
source.put("loo_alphabravo_gamma", "delta");
SystemEnvironmentPropertySource propertySource = new SystemEnvironmentPropertySource(
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, source);
SpringIterableConfigurationPropertySource adapter = new SpringIterableConfigurationPropertySource(
@ -166,6 +168,27 @@ class SpringIterableConfigurationPropertySourceTests { @@ -166,6 +168,27 @@ class SpringIterableConfigurationPropertySourceTests {
.isEqualTo(ConfigurationPropertyState.PRESENT);
assertThat(adapter.containsDescendantOf(ConfigurationPropertyName.of("foo.blah")))
.isEqualTo(ConfigurationPropertyState.ABSENT);
assertThat(adapter.containsDescendantOf(ConfigurationPropertyName.of("loo.bar-baz")))
.isEqualTo(ConfigurationPropertyState.PRESENT);
assertThat(adapter.containsDescendantOf(ConfigurationPropertyName.of("loo.alpha-bravo")))
.isEqualTo(ConfigurationPropertyState.PRESENT);
assertThat(adapter.containsDescendantOf(ConfigurationPropertyName.of("loo.blah")))
.isEqualTo(ConfigurationPropertyState.ABSENT);
}
@Test
void getConfigurationPropertyWhenSystemEnvironmentPropertySourceShouldSupportLegacyProperty() {
Map<String, Object> source = new LinkedHashMap<>();
source.put("TEST_VALUE_UPPER", "upper");
source.put("test_value_lower", "lower");
SystemEnvironmentPropertySource propertySource = new SystemEnvironmentPropertySource(
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, source);
SpringIterableConfigurationPropertySource adapter = new SpringIterableConfigurationPropertySource(
propertySource, true, SystemEnvironmentPropertyMapper.INSTANCE, DefaultPropertyMapper.INSTANCE);
assertThat(adapter.getConfigurationProperty(ConfigurationPropertyName.of("test.value-upper")).getValue())
.isEqualTo("upper");
assertThat(adapter.getConfigurationProperty(ConfigurationPropertyName.of("test.value-lower")).getValue())
.isEqualTo("lower");
}
@Test

Loading…
Cancel
Save