Skip to content

Commit

Permalink
Register all profile properties in the default config source.
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Mar 2, 2021
1 parent dad1c11 commit 8d85f22
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ ReadResult run() {
nameBuilder.setLength(len);
}
// sweep-up
for (String propertyName : config.getPropertyNames()) {
for (String propertyName : getAllProperties()) {
if (propertyName.equals(ConfigSource.CONFIG_ORDINAL)) {
continue;
}
Expand Down Expand Up @@ -718,6 +718,19 @@ private Converter<?> getConverter(SmallRyeConfig config, Field field, ConverterT
convByType.put(valueType, converter);
return converter;
}

/**
* We collect all properties from ConfigSources, because Config#getPropertyNames exclude the active profiled
* properties, meaning that the property is written in the default config source unprofiled. This may cause
* issues if we run with a different profile and fallback to defaults.
*/
private Set<String> getAllProperties() {
Set<String> properties = new HashSet<>();
for (ConfigSource configSource : config.getConfigSources()) {
properties.addAll(configSource.getPropertyNames());
}
return properties;
}
}

public static final class ReadResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@

import io.quarkus.test.QuarkusUnitTest;

public class ConfigDefaultValues {
public class ConfigDefaultValuesTest {
@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addAsResource(new StringAsset("config_ordinal=1000\n" +
"my.prop=1234\n"), "application.properties"));
.addAsResource(new StringAsset(
"config_ordinal=1000\n" +
"my.prop=1234\n" +
"%prod.my.prop=1234\n" +
"%dev.my.prop=5678\n" +
"%test.my.prop=1234"),
"application.properties"));
@Inject
Config config;

Expand All @@ -38,6 +43,17 @@ void configDefaultValues() {
assertEquals("1234", applicationProperties.getValue("my.prop"));
}

@Test
void profileDefaultValues() {
ConfigSource defaultValues = getConfigSourceByName("PropertiesConfigSource[source=Specified default values]");
assertNotNull(defaultValues);
assertEquals("1234", defaultValues.getValue("my.prop"));
assertEquals("1234", defaultValues.getValue("%prod.my.prop"));
assertEquals("5678", defaultValues.getValue("%dev.my.prop"));
assertEquals("1234", defaultValues.getValue("%test.my.prop"));
assertEquals("1234", config.getValue("my.prop", String.class));
}

private ConfigSource getConfigSourceByName(String name) {
for (ConfigSource configSource : config.getConfigSources()) {
if (configSource.getName().contains(name)) {
Expand Down

0 comments on commit 8d85f22

Please sign in to comment.