Skip to content

Commit

Permalink
Do not transform properties names coming from .env to underscores (#1183
Browse files Browse the repository at this point in the history
)
  • Loading branch information
radcortez authored Jun 27, 2024
1 parent fcebd84 commit 64295f6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,11 +30,7 @@ protected String[] getFileExtensions() {

@Override
protected ConfigSource loadConfigSource(final URL url, final int ordinal) throws IOException {
Map<String, String> envProperties = new HashMap<>();
for (final Map.Entry<String, String> 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 + "]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

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;

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;
Expand Down Expand Up @@ -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<String> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -318,15 +319,35 @@ 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();

DashedEnvNames mapping = config.getConfigMapping(DashedEnvNames.class);

assertEquals("value", mapping.value());
assertEquals("value", mapping.nested().get("dashed-key").another());

Set<String> 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")
Expand Down

0 comments on commit 64295f6

Please sign in to comment.