Skip to content

Commit

Permalink
Do not override defaults set by the builder with mappings (#1013)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Sep 28, 2023
1 parent 1294b93 commit e5bdb10
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -950,11 +950,11 @@ private void addDefault(ArrayDeque<String> path, String value) {
}

ConfigMappingContext mapConfiguration(SmallRyeConfig config) throws ConfigValidationException {
for (ConfigSource configSource : config.getConfigSources()) {
if (configSource instanceof DefaultValuesConfigSource) {
DefaultValuesConfigSource defaultValuesConfigSource = (DefaultValuesConfigSource) configSource;
defaultValuesConfigSource.addDefaults(defaultValues);
}
// We need to set defaults from mappings here, because in a CDI environment mappings are added on an existent Config instance
ConfigSource configSource = config.getDefaultValues();
if (configSource instanceof DefaultValuesConfigSource) {
DefaultValuesConfigSource defaultValuesConfigSource = (DefaultValuesConfigSource) configSource;
defaultValuesConfigSource.addDefaults(defaultValues);
}
matchPropertiesWithEnv(config, roots.keySet(), getProperties().keySet());
return SecretKeys.doUnlocked(() -> mapConfigurationInternal(config));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ public String getValue(final String propertyName) {
void addDefaults(final Map<String, String> properties) {
for (Map.Entry<String, String> entry : properties.entrySet()) {
if (entry.getKey().indexOf('*') == -1) {
this.properties.put(entry.getKey(), entry.getValue());
this.properties.putIfAbsent(entry.getKey(), entry.getValue());
} else {
this.wildcards.findOrAdd(entry.getKey()).putRootValue(entry.getValue());
KeyMap<String> key = this.wildcards.findOrAdd(entry.getKey());
if (!key.hasRootValue()) {
key.putRootValue(entry.getValue());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,11 +534,16 @@ public List<String> getProfiles() {
return configSources.getProfiles();
}

public ConfigSource getDefaultValues() {
return configSources.defaultValues;
}

private static class ConfigSources implements Serializable {
private static final long serialVersionUID = 3483018375584151712L;

private final List<String> profiles;
private final List<ConfigSource> sources;
private final ConfigSource defaultValues;
private final ConfigSourceInterceptorContext interceptorChain;
private final PropertyNames propertyNames;

Expand All @@ -550,6 +555,10 @@ private static class ConfigSources implements Serializable {
ConfigSources(final SmallRyeConfigBuilder builder) {
// Add all sources except for ConfigurableConfigSource types. These are initialized later
List<ConfigSource> sources = buildSources(builder);
// Add the default values sources separately, so we can keep a reference to it and add mappings defaults
DefaultValuesConfigSource defaultValues = new DefaultValuesConfigSource(builder.getDefaultValues());
sources.add(defaultValues);

// Add all interceptors
List<ConfigSourceInterceptor> interceptors = new ArrayList<>();
List<InterceptorWithPriority> interceptorWithPriorities = buildInterceptors(builder);
Expand Down Expand Up @@ -580,6 +589,7 @@ private static class ConfigSources implements Serializable {

this.profiles = profiles;
this.sources = configSources;
this.defaultValues = defaultValues;
this.interceptorChain = current;
this.propertyNames = new PropertyNames();
}
Expand All @@ -598,7 +608,6 @@ private static List<ConfigSource> buildSources(final SmallRyeConfigBuilder build
if (builder.isAddDefaultSources()) {
sourcesToBuild.addAll(builder.getDefaultSources());
}
sourcesToBuild.add(new DefaultValuesConfigSource(builder.getDefaultValues()));

return sourcesToBuild;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,6 @@ public SmallRyeConfig build() {
.forEach(customizer -> customizer.configBuilder(SmallRyeConfigBuilder.this));

ConfigMappingProvider mappingProvider = mappingsBuilder.build();
defaultValues.putAll(mappingProvider.getDefaultValues());

SmallRyeConfig config = new SmallRyeConfig(this);
ConfigMappings.mapConfiguration(config, mappingProvider);
return config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2372,4 +2372,34 @@ interface Nested {
String name();
}
}

@Test
void doNotOverrideBuilderDefault() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withDefaultValue("override.value", "another")
.withDefaultValue("override.map.key.value", "another")
.withMapping(DoNotOverrideBuilderDefault.class)
.build();

assertEquals("another", config.getRawValue("override.value"));
assertEquals("another", config.getRawValue("override.map.key.value"));

DoNotOverrideBuilderDefault mapping = config.getConfigMapping(DoNotOverrideBuilderDefault.class);
assertEquals("another", mapping.value());
assertEquals("another", mapping.map().get("key").value());
}

@ConfigMapping(prefix = "override")
interface DoNotOverrideBuilderDefault {
@WithDefault("value")
String value();

@WithDefaults
Map<String, Nested> map();

interface Nested {
@WithDefault("value")
String value();
}
}
}

0 comments on commit e5bdb10

Please sign in to comment.