From 5e630816fe041a7e0d6bb25ed709931e0424ba47 Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Thu, 9 Nov 2023 18:51:35 +0000 Subject: [PATCH] Do not remove dotted property names from EnvSource when matching ConfigMapping patterns --- .../config/ConfigMappingProvider.java | 7 +++- .../smallrye/config/EnvConfigSourceTest.java | 42 +++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/implementation/src/main/java/io/smallrye/config/ConfigMappingProvider.java b/implementation/src/main/java/io/smallrye/config/ConfigMappingProvider.java index 1d7c321d8..d700c5f5e 100644 --- a/implementation/src/main/java/io/smallrye/config/ConfigMappingProvider.java +++ b/implementation/src/main/java/io/smallrye/config/ConfigMappingProvider.java @@ -1194,8 +1194,11 @@ private static void matchPropertiesWithEnv(final SmallRyeConfig config, final Se for (Integer dash : indexOfDashes) { sb.setCharAt(dash, '-'); } - envConfigSource.getPropertyNames().add(sb.toString()); - envConfigSource.getPropertyNames().remove(envProperty); + String expectedEnvProperty = sb.toString(); + if (!envProperty.equals(expectedEnvProperty)) { + envConfigSource.getPropertyNames().add(sb.toString()); + envConfigSource.getPropertyNames().remove(envProperty); + } sb.setLength(0); break; } diff --git a/implementation/src/test/java/io/smallrye/config/EnvConfigSourceTest.java b/implementation/src/test/java/io/smallrye/config/EnvConfigSourceTest.java index dfcf0b62b..ad0912d39 100644 --- a/implementation/src/test/java/io/smallrye/config/EnvConfigSourceTest.java +++ b/implementation/src/test/java/io/smallrye/config/EnvConfigSourceTest.java @@ -239,6 +239,48 @@ void sameNames() { assertEquals("upper", envConfigSource.getValue("MY_STRING_PROPERTY")); } + @Test + void dashedEnvNames() { + SmallRyeConfig config = new SmallRyeConfigBuilder() + .withMapping(DashedEnvNames.class) + .withSources(new EnvConfigSource(Map.of( + "DASHED_ENV_NAMES_VALUE", "value", + "DASHED_ENV_NAMES_NESTED__DASHED_KEY__ANOTHER", "value"), 100)) + .build(); + + DashedEnvNames mapping = config.getConfigMapping(DashedEnvNames.class); + + assertEquals("value", mapping.value()); + // Unfortunately, we still don't have a good way to determine if the Map key is dashed or not + assertEquals("value", mapping.nested().get("dashed.key").another()); + } + + @Test + void dottedDashedEnvNames() { + SmallRyeConfig config = new SmallRyeConfigBuilder() + .withMapping(DashedEnvNames.class) + .withSources(new EnvConfigSource(Map.of( + "dashed-env-names.value", "value", + "dashed-env-names.nested.dashed-key.another", "value"), 100)) + .build(); + + DashedEnvNames mapping = config.getConfigMapping(DashedEnvNames.class); + + assertEquals("value", mapping.value()); + assertEquals("value", mapping.nested().get("dashed-key").another()); + } + + @ConfigMapping(prefix = "dashed-env-names") + interface DashedEnvNames { + String value(); + + Map nested(); + + interface Nested { + String another(); + } + } + private static boolean envSourceEquals(String name, String lookup) { return BOOLEAN_CONVERTER.convert(new EnvConfigSource(Map.of(name, "true"), 100).getValue(lookup)); }