Skip to content

Commit

Permalink
Merge pull request #40660 from radcortez/3.10.1-backport-40516
Browse files Browse the repository at this point in the history
[3.10] Always record original default values
  • Loading branch information
gsmet authored May 17, 2024
2 parents 62064eb + 0b17dd6 commit a45c652
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1233,12 +1233,12 @@ private static void getDefaults(
ClassDefinition.ItemMember itemMember = (ClassDefinition.ItemMember) member;
String defaultValue = itemMember.getDefaultValue();
if (defaultValue != null) {
// lookup config to make sure we catch relocates or fallbacks
// lookup config to make sure we catch relocates or fallbacks and override the value
ConfigValue configValue = config.getConfigValue(propertyName.toString());
if (configValue.getValue() != null) {
defaultValues.put(configValue.getName(), configValue.getValue());
if (configValue.getValue() != null && !configValue.getName().equals(propertyName.toString())) {
defaultValues.put(propertyName.toString(), configValue.getValue());
} else {
defaultValues.put(configValue.getName(), defaultValue);
defaultValues.put(propertyName.toString(), defaultValue);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,16 @@ void generateBuilders(
BuildProducer<ReflectiveClassBuildItem> reflectiveClass) throws Exception {

Map<String, String> defaultValues = new HashMap<>();
Map<String, String> mappingsDefaults = new HashMap<>();
// Default values from extensions @ConfigMapping
for (ConfigClassWithPrefix runTimeMapping : configItem.getReadResult().getRunTimeMappings()) {
defaultValues.putAll(ConfigMappings.getDefaults(runTimeMapping));
mappingsDefaults.putAll(ConfigMappings.getDefaults(runTimeMapping));
}
// Default values from user @ConfigMapping
for (ConfigMappingBuildItem configMapping : configMappings) {
defaultValues.putAll(ConfigMappings.getDefaults(configMapping.toConfigClassWithPrefix()));
mappingsDefaults.putAll(ConfigMappings.getDefaults(configMapping.toConfigClassWithPrefix()));
}
defaultValues.putAll(mappingsDefaults);
// Default values from @ConfigRoot
defaultValues.putAll(configItem.getReadResult().getRunTimeDefaultValues());
// Default values from build item RunTimeConfigurationDefaultBuildItem override
Expand All @@ -223,7 +225,11 @@ void generateBuilders(
// Runtime values may contain active profiled names that override sames names in defaults
// We need to keep the original name definition in case a different profile is used to run the app
String activeName = ProfileConfigSourceInterceptor.activeName(entry.getKey(), profiles);
defaultValues.remove(activeName);
// But keep the default
if (!configItem.getReadResult().getRunTimeDefaultValues().containsKey(activeName)
&& !mappingsDefaults.containsKey(activeName)) {
defaultValues.remove(activeName);
}
defaultValues.put(entry.getKey(), entry.getValue());
}
defaultValues.putAll(configItem.getReadResult().getRunTimeValues());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ quarkus.arc.unremovable-types[0]=foo
### recording
bt.ok.to.record=from-app
%test.bt.profile.record=properties
%test.quarkus.mapping.rt.record-default=from-app
%test.quarkus.rt.record-default=from-app

### mappings
quarkus.mapping.bt.value=value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ void doNotRecordActiveUnprofiledPropertiesDefaults() {
assertNull(defaultValues.get().getValue("bt.profile.record"));
}

@Test
void recordDefaultFromRootEvenIfInActiveProfile() {
Optional<ConfigSource> defaultValues = config.getConfigSource("DefaultValuesConfigSource");
assertTrue(defaultValues.isPresent());

// Old Roots
assertEquals("from-app", defaultValues.get().getValue("%test.quarkus.rt.record-default"));
assertEquals("from-default", defaultValues.get().getValue("quarkus.rt.record-default"));
// Mappings
assertEquals("from-app", defaultValues.get().getValue("%test.quarkus.mapping.rt.record-default"));
assertEquals("from-default", defaultValues.get().getValue("quarkus.mapping.rt.record-default"));
}

@Test
void recordProfile() {
Optional<ConfigSource> defaultValues = config.getConfigSource("DefaultValuesConfigSource");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;

@ConfigMapping(prefix = "quarkus.mapping.rt")
@ConfigRoot(phase = ConfigPhase.RUN_TIME)
Expand All @@ -25,6 +26,12 @@ public interface TestMappingRunTime {
/** Record values with named profile **/
Optional<String> recordProfiled();

/**
* Record Default
*/
@WithDefault("from-default")
String recordDefault();

interface Group {
/**
* A Group value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ public class TestRunTimeConfig {
@ConfigItem
public Optional<String> doNotRecord;

/**
* Record Default
*/
@ConfigItem(defaultValue = "from-default")
public String recordDefault;

@Override
public String toString() {
return "TestRunTimeConfig{" +
Expand Down

0 comments on commit a45c652

Please sign in to comment.