From 4301186a95ff06ea9c33e39087563b77f2b2f00a Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 9 Jun 2025 17:53:48 -0700 Subject: [PATCH] Fix SystemEnvironmentPropertyMapper tests and remove duplicate names See gh-45741 --- There | 0 .../source/SystemEnvironmentPropertyMapper.java | 17 +++++++++++++---- .../SystemEnvironmentPropertyMapperTests.java | 15 ++++++++------- 3 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 There diff --git a/There b/There new file mode 100644 index 00000000000..e69de29bb2d diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java index b2e301dd59f..a840ae5de5b 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java @@ -16,6 +16,7 @@ package org.springframework.boot.context.properties.source; +import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.function.BiPredicate; @@ -40,10 +41,18 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper { @Override public List map(ConfigurationPropertyName configurationPropertyName) { - 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)); + List mapped = new ArrayList<>(4); + addIfMissing(mapped, configurationPropertyName.toString(ToStringFormat.SYSTEM_ENVIRONMENT, true)); + addIfMissing(mapped, configurationPropertyName.toString(ToStringFormat.LEGACY_SYSTEM_ENVIRONMENT, true)); + addIfMissing(mapped, configurationPropertyName.toString(ToStringFormat.SYSTEM_ENVIRONMENT, false)); + addIfMissing(mapped, configurationPropertyName.toString(ToStringFormat.LEGACY_SYSTEM_ENVIRONMENT, false)); + return mapped; + } + + private void addIfMissing(List list, String value) { + if (!list.contains(value)) { + list.add(value); + } } @Override diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapperTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapperTests.java index 74902fe6853..ab8fdfe0c1c 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapperTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapperTests.java @@ -48,13 +48,14 @@ class SystemEnvironmentPropertyMapperTests extends AbstractPropertyMapperTests { @Test void mapFromConfigurationShouldReturnBestGuess() { - assertThat(mapConfigurationPropertyName("server")).containsExactly("SERVER"); - assertThat(mapConfigurationPropertyName("server.port")).containsExactly("SERVER_PORT"); - assertThat(mapConfigurationPropertyName("host[0]")).containsExactly("HOST_0"); - assertThat(mapConfigurationPropertyName("host[0][1]")).containsExactly("HOST_0_1"); - assertThat(mapConfigurationPropertyName("host[0].name")).containsExactly("HOST_0_NAME"); - assertThat(mapConfigurationPropertyName("host.f00.name")).containsExactly("HOST_F00_NAME"); - assertThat(mapConfigurationPropertyName("foo.the-bar")).containsExactly("FOO_THEBAR", "FOO_THE_BAR"); + assertThat(mapConfigurationPropertyName("server")).containsExactly("SERVER", "server"); + assertThat(mapConfigurationPropertyName("server.port")).containsExactly("SERVER_PORT", "server_port"); + assertThat(mapConfigurationPropertyName("host[0]")).containsExactly("HOST_0", "host_0"); + assertThat(mapConfigurationPropertyName("host[0][1]")).containsExactly("HOST_0_1", "host_0_1"); + assertThat(mapConfigurationPropertyName("host[0].name")).containsExactly("HOST_0_NAME", "host_0_name"); + assertThat(mapConfigurationPropertyName("host.f00.name")).containsExactly("HOST_F00_NAME", "host_f00_name"); + assertThat(mapConfigurationPropertyName("foo.the-bar")).containsExactly("FOO_THEBAR", "FOO_THE_BAR", + "foo_thebar", "foo_the_bar"); } @Test