Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not transform properties names coming from .env to underscores #1183

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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