diff --git a/implementation/src/main/java/io/smallrye/config/ConfigMappingProvider.java b/implementation/src/main/java/io/smallrye/config/ConfigMappingProvider.java index bb2a6acc5..757cf0c85 100644 --- a/implementation/src/main/java/io/smallrye/config/ConfigMappingProvider.java +++ b/implementation/src/main/java/io/smallrye/config/ConfigMappingProvider.java @@ -9,6 +9,8 @@ import static io.smallrye.config.ConfigMappingLoader.getConfigMapping; import static io.smallrye.config.ConfigMappingLoader.getConfigMappingClass; import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_MAPPING_VALIDATE_UNKNOWN; +import static io.smallrye.config.common.utils.StringUtil.replaceNonAlphanumericByUnderscores; +import static io.smallrye.config.common.utils.StringUtil.toLowerCaseAndDotted; import static java.lang.Integer.parseInt; import java.io.Serializable; @@ -1094,8 +1096,11 @@ private static void matchPropertiesWithEnv(final SmallRyeConfig config, final Se for (String configuredProperty : configuredProperties) { Set envNames = envConfigSource.getPropertyNames(); if (envConfigSource.hasPropertyName(configuredProperty)) { - envNames.remove(configuredProperty); - envNames.add(configuredProperty); + if (!envNames.contains(configuredProperty)) { + // this may be expensive, but it shouldn't happend that often + envNames.remove(toLowerCaseAndDotted(replaceNonAlphanumericByUnderscores(configuredProperty))); + envNames.add(configuredProperty); + } } } } diff --git a/implementation/src/test/java/io/smallrye/config/EnvConfigSourceTest.java b/implementation/src/test/java/io/smallrye/config/EnvConfigSourceTest.java index 34c94e2e3..39f769e90 100644 --- a/implementation/src/test/java/io/smallrye/config/EnvConfigSourceTest.java +++ b/implementation/src/test/java/io/smallrye/config/EnvConfigSourceTest.java @@ -18,6 +18,7 @@ import static io.smallrye.config.Converters.STRING_CONVERTER; import static io.smallrye.config.KeyValuesConfigSource.config; import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toSet; import static java.util.stream.StreamSupport.stream; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -29,7 +30,8 @@ import java.util.HashMap; import java.util.Map; import java.util.NoSuchElementException; -import java.util.stream.StreamSupport; +import java.util.Optional; +import java.util.Set; import org.eclipse.microprofile.config.spi.ConfigSource; import org.junit.jupiter.api.Test; @@ -88,12 +90,10 @@ void empty() { assertTrue( stream(config.getPropertyNames().spliterator(), false).collect(toList()).contains("SMALLRYE_MP_CONFIG_EMPTY")); - ConfigSource envConfigSource = StreamSupport.stream(config.getConfigSources().spliterator(), false) - .filter(configSource -> configSource.getName().equals("EnvConfigSource")) - .findFirst() - .get(); + Optional envConfigSource = config.getConfigSource("EnvConfigSource"); - assertEquals("", envConfigSource.getValue("SMALLRYE_MP_CONFIG_EMPTY")); + assertTrue(envConfigSource.isPresent()); + assertEquals("", envConfigSource.get().getValue("SMALLRYE_MP_CONFIG_EMPTY")); } @Test @@ -107,7 +107,7 @@ void ordinal() { @Test void indexed() { - Map env = new HashMap() { + Map env = new HashMap<>() { { put("INDEXED_0_", "foo"); put("INDEXED_0__PROP", "bar"); @@ -142,7 +142,7 @@ void numbers() { @Test void map() { - Map env = new HashMap() { + Map env = new HashMap<>() { { put("TEST_LANGUAGE__DE_ETR__", "Einfache Sprache"); put("TEST_LANGUAGE_PT_BR", "FROM ENV"); @@ -217,4 +217,19 @@ void envEquals() { assertEquals(new EnvProperty("TEST_LANGUAGE__DE_ETR_").hashCode(), new EnvProperty("test.language.\"de.etr\"").hashCode()); } + + @Test + void sameSemanticMeaning() { + SmallRyeConfig config = new SmallRyeConfigBuilder() + .withSources(config("foo.bar-baz", "fromOther")) + .withSources(new EnvConfigSource(Map.of("FOO_BAR_BAZ", "fromEnv"), 300)) + .build(); + + Set properties = stream(config.getPropertyNames().spliterator(), false).collect(toSet()); + assertTrue(properties.contains("FOO_BAR_BAZ")); + assertTrue(properties.contains("foo.bar-baz")); + assertFalse(properties.contains("foo.bar.baz")); + + assertEquals("fromEnv", config.getRawValue("foo.bar-baz")); + } }