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

Env matching names before mappings #1180

Merged
merged 1 commit into from
Jun 19, 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 @@ -85,11 +85,11 @@ public class SmallRyeConfig implements Config, Serializable {
this.configSources = new ConfigSources(builder, this);
this.converters = buildConverters(builder);
this.configValidator = builder.getValidator();
this.mappings = new ConcurrentHashMap<>(buildMappings(builder));

// Match dotted properties from other sources with Env with the same semantic meaning
// This needs to happen after matching dashed names from mappings
// This needs to happen before matching dashed names from mappings
matchPropertiesWithEnv();
this.mappings = new ConcurrentHashMap<>(buildMappings(builder));
}

private Map<Type, Converter<?>> buildConverters(final SmallRyeConfigBuilder builder) {
Expand Down Expand Up @@ -159,8 +159,8 @@ public String apply(final String name) {

for (ConfigSource configSource : getConfigSources(EnvConfigSource.class)) {
EnvConfigSource envConfigSource = (EnvConfigSource) configSource;
Set<String> envNames = envConfigSource.getPropertyNames();
for (String dottedProperty : dottedProperties) {
Set<String> envNames = envConfigSource.getPropertyNames();
if (envConfigSource.hasPropertyName(dottedProperty)) {
if (!envNames.contains(dottedProperty)) {
// this may be expensive, but it shouldn't happen that often
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,26 @@ interface MappingFactory {
Map<String, String> aMap();
}

@Test
void propertyNamesCache() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(config("mp.messaging.incoming.words-in.topic", "from-properties"))
.withSources(new EnvConfigSource(Map.of("MP_MESSAGING_INCOMING_WORDS_IN_TOPIC", "from-env"), 300))
.withMapping(PropertyNamesCache.class)
.build();

Set<String> properties = stream(config.getPropertyNames().spliterator(), false).collect(toSet());
assertEquals(2, properties.size());
assertTrue(properties.contains("mp.messaging.incoming.words-in.topic"));
assertTrue(properties.contains("MP_MESSAGING_INCOMING_WORDS_IN_TOPIC"));
assertFalse(properties.contains("mp.messaging.incoming.words.in.topic"));
}

@ConfigMapping
interface PropertyNamesCache {
Map<String, String> values();
}

private static boolean envSourceEquals(String name, String lookup) {
return BOOLEAN_CONVERTER.convert(new EnvConfigSource(Map.of(name, "true"), 100).getValue(lookup));
}
Expand Down