From f1e44ea6cd792ec89ea7f8c33783ab2dff456564 Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Thu, 27 Jun 2024 21:27:23 +0100 Subject: [PATCH] Do not transform properties names coming from .env to underscores --- .../config/DotEnvConfigSourceProvider.java | 9 +----- .../DotEnvConfigSourceProviderTest.java | 29 +++++++++++++++++++ .../smallrye/config/EnvConfigSourceTest.java | 23 ++++++++++++++- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/implementation/src/main/java/io/smallrye/config/DotEnvConfigSourceProvider.java b/implementation/src/main/java/io/smallrye/config/DotEnvConfigSourceProvider.java index 7ccff65f6..7c4007beb 100644 --- a/implementation/src/main/java/io/smallrye/config/DotEnvConfigSourceProvider.java +++ b/implementation/src/main/java/io/smallrye/config/DotEnvConfigSourceProvider.java @@ -5,15 +5,12 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.HashMap; import java.util.List; -import java.util.Map; import org.eclipse.microprofile.config.spi.ConfigSource; import org.eclipse.microprofile.config.spi.ConfigSourceProvider; import io.smallrye.config.common.utils.ConfigSourceUtil; -import io.smallrye.config.common.utils.StringUtil; public class DotEnvConfigSourceProvider extends AbstractLocationConfigSourceLoader implements ConfigSourceProvider { private final String location; @@ -33,11 +30,7 @@ protected String[] getFileExtensions() { @Override protected ConfigSource loadConfigSource(final URL url, final int ordinal) throws IOException { - Map envProperties = new HashMap<>(); - for (final Map.Entry entry : ConfigSourceUtil.urlToMap(url).entrySet()) { - envProperties.put(StringUtil.replaceNonAlphanumericByUnderscores(entry.getKey()), entry.getValue()); - } - return new EnvConfigSource(envProperties, ordinal) { + return new EnvConfigSource(ConfigSourceUtil.urlToMap(url), ordinal) { @Override public String getName() { return super.getName() + "[source=" + url + "]"; diff --git a/implementation/src/test/java/io/smallrye/config/DotEnvConfigSourceProviderTest.java b/implementation/src/test/java/io/smallrye/config/DotEnvConfigSourceProviderTest.java index e86326d03..96bfdfe0e 100644 --- a/implementation/src/test/java/io/smallrye/config/DotEnvConfigSourceProviderTest.java +++ b/implementation/src/test/java/io/smallrye/config/DotEnvConfigSourceProviderTest.java @@ -2,6 +2,9 @@ import static io.smallrye.config.DotEnvConfigSourceProvider.dotEnvSources; import static io.smallrye.config.SecuritySupport.getContextClassLoader; +import static java.util.Collections.emptyMap; +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.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -9,6 +12,7 @@ import java.io.FileOutputStream; import java.nio.file.Path; import java.util.Properties; +import java.util.Set; import org.eclipse.microprofile.config.spi.ConfigSource; import org.junit.jupiter.api.Test; @@ -108,4 +112,29 @@ protected boolean failOnMissingFile() { assertThrows(IllegalArgumentException.class, () -> failBuilder.build()); } + + @Test + void dottedDashedEnvNames(@TempDir Path tempDir) throws Exception { + Properties envProperties = new Properties(); + envProperties.setProperty("_DEV_DASHED_ENV_NAMES_DASHED_NAME", "value"); + try (FileOutputStream out = new FileOutputStream(tempDir.resolve(".env").toFile())) { + envProperties.store(out, null); + } + + SmallRyeConfig config = new SmallRyeConfigBuilder() + .withMapping(DashedEnvNames.class) + .withSources(new EnvConfigSource(emptyMap(), 300)) + .withSources(dotEnvSources(tempDir.resolve(".env").toFile().toURI().toString(), getContextClassLoader())) + .withProfile("dev") + .build(); + + Set properties = stream(config.getPropertyNames().spliterator(), false).collect(toSet()); + assertTrue(properties.contains("dashed-env-names.dashed-name")); + assertTrue(properties.contains("_DEV_DASHED_ENV_NAMES_DASHED_NAME")); + } + + @ConfigMapping(prefix = "dashed-env-names") + interface DashedEnvNames { + String dashedName(); + } } diff --git a/implementation/src/test/java/io/smallrye/config/EnvConfigSourceTest.java b/implementation/src/test/java/io/smallrye/config/EnvConfigSourceTest.java index b767f17a7..0527ad0d8 100644 --- a/implementation/src/test/java/io/smallrye/config/EnvConfigSourceTest.java +++ b/implementation/src/test/java/io/smallrye/config/EnvConfigSourceTest.java @@ -19,6 +19,7 @@ import static io.smallrye.config.Converters.STRING_CONVERTER; import static io.smallrye.config.KeyValuesConfigSource.config; import static java.util.Collections.emptyList; +import static java.util.Collections.emptyMap; import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toSet; import static java.util.stream.StreamSupport.stream; @@ -318,8 +319,9 @@ void dashedEnvNames() { void dottedDashedEnvNames() { SmallRyeConfig config = new SmallRyeConfigBuilder() .withMapping(DashedEnvNames.class) + .withSources(new EnvConfigSource(emptyMap(), 300)) .withSources(new EnvConfigSource(Map.of( - "dashed-env-names.value", "value", + "DASHED_ENV_NAMES_VALUE", "value", "dashed-env-names.nested.dashed-key.another", "value"), 100)) .build(); @@ -327,6 +329,25 @@ void dottedDashedEnvNames() { assertEquals("value", mapping.value()); assertEquals("value", mapping.nested().get("dashed-key").another()); + + Set properties = stream(config.getPropertyNames().spliterator(), false).collect(toSet()); + assertTrue(properties.contains("dashed-env-names.value")); + assertTrue(properties.contains("DASHED_ENV_NAMES_VALUE")); + assertFalse(properties.contains("dashed.env.names.value")); + assertTrue(properties.contains("dashed-env-names.nested.dashed-key.another")); + + config = new SmallRyeConfigBuilder() + .withMapping(DashedEnvNames.class) + .withSources(new EnvConfigSource(emptyMap(), 300)) + .withSources(new EnvConfigSource(Map.of( + "%DEV_DASHED_ENV_NAMES_VALUE", "value"), 100)) + .withProfile("dev") + .build(); + + properties = stream(config.getPropertyNames().spliterator(), false).collect(toSet()); + assertTrue(properties.contains("dashed-env-names.value")); + assertTrue(properties.contains("%DEV_DASHED_ENV_NAMES_VALUE")); + assertFalse(properties.contains("dashed.env.names.value")); } @ConfigMapping(prefix = "dashed-env-names")