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 record active profile configuration name if a profile one exists #39426

Merged
merged 3 commits into from
Mar 14, 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 @@ -78,6 +78,7 @@
import io.smallrye.config.EnvConfigSource;
import io.smallrye.config.KeyMap;
import io.smallrye.config.KeyMapBackedConfigSource;
import io.smallrye.config.ProfileConfigSourceInterceptor;
import io.smallrye.config.PropertiesConfigSource;
import io.smallrye.config.SecretKeys;
import io.smallrye.config.SmallRyeConfig;
Expand Down Expand Up @@ -640,7 +641,7 @@ ReadResult run() {
unknownBuildProperties.remove(property);
ConfigValue value = config.getConfigValue(property);
if (value != null && value.getRawValue() != null) {
allBuildTimeValues.put(property, value.getRawValue());
allBuildTimeValues.put(value.getNameProfiled(), value.getRawValue());
}
}
}
Expand All @@ -652,8 +653,8 @@ ReadResult run() {
unknownBuildProperties.remove(property);
ConfigValue value = config.getConfigValue(property);
if (value != null && value.getRawValue() != null) {
allBuildTimeValues.put(property, value.getRawValue());
buildTimeRunTimeValues.put(property, value.getRawValue());
allBuildTimeValues.put(value.getNameProfiled(), value.getRawValue());
buildTimeRunTimeValues.put(value.getNameProfiled(), value.getRawValue());
}
}
}
Expand All @@ -665,7 +666,7 @@ ReadResult run() {
unknownBuildProperties.remove(property);
ConfigValue value = config.getConfigValue(property);
if (value != null && value.getRawValue() != null) {
runTimeValues.put(property, value.getRawValue());
runTimeValues.put(value.getNameProfiled(), value.getRawValue());
}
}
}
Expand Down Expand Up @@ -1073,8 +1074,7 @@ public String getValue(final String propertyName) {

Set<String> properties = new HashSet<>();

// We build a new Config to also apply the interceptor chain, this includes the active profile. A property
// may only exist in its profiled name format, but it requires recording in the unprofiled name
// We build a new Config to also apply the interceptor chain to generate any additional properties.
SmallRyeConfigBuilder builder = ConfigUtils.emptyConfigBuilder();
builder.getSources().clear();
builder.getSourceProviders().clear();
Expand Down Expand Up @@ -1108,8 +1108,10 @@ public Set<String> getPropertyNames() {
return Collections.emptySet();
}
});

String[] profiles = config.getProfiles().toArray(String[]::new);
for (String property : builder.build().getPropertyNames()) {
properties.add(property);
properties.add(ProfileConfigSourceInterceptor.activeName(property, profiles));
}

return properties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
import io.smallrye.config.ConfigSourceInterceptor;
import io.smallrye.config.ConfigSourceInterceptorFactory;
import io.smallrye.config.DefaultValuesConfigSource;
import io.smallrye.config.ProfileConfigSourceInterceptor;
import io.smallrye.config.SecretKeysHandler;
import io.smallrye.config.SecretKeysHandlerFactory;
import io.smallrye.config.SmallRyeConfig;
Expand Down Expand Up @@ -217,6 +218,14 @@ void generateBuilders(
defaultValues.put(e.getKey(), e.getValue());
}
// Recorded values from build time from any other source (higher ordinal then defaults, so override)
String[] profiles = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class).getProfiles().toArray(String[]::new);
for (Map.Entry<String, String> entry : configItem.getReadResult().getRunTimeValues().entrySet()) {
// 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);
defaultValues.put(entry.getKey(), entry.getValue());
}
defaultValues.putAll(configItem.getReadResult().getRunTimeValues());

Set<String> converters = discoverService(Converter.class, reflectiveClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public void configBuilder(final SmallRyeConfigBuilder builder) {
new PropertiesConfigSource(Map.of("prop.recorded.from.btconfigsource", "1234"), "BuildTimeConfigSource", 100));
// for RecorderRuntimeConfigTest
builder.withSources(
new PropertiesConfigSource(Map.of("recorded.property", "from-application"), "BuildTimeConfigSource", 250));
new PropertiesConfigSource(Map.of(
"recorded.property", "from-application",
"%test.recorded.profiled.property", "from-application",
"%test.quarkus.mapping.rt.record-profiled", "from-application"), "BuildTimeConfigSource", 250));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,10 @@ void neverRunThisOne() {
@BuildStep
void recordProperty(BuildProducer<RunTimeConfigurationDefaultBuildItem> runTimeConfigurationDefault) {
runTimeConfigurationDefault.produce(new RunTimeConfigurationDefaultBuildItem("recorded.property", "from-build-step"));
runTimeConfigurationDefault
.produce(new RunTimeConfigurationDefaultBuildItem("recorded.profiled.property", "should-not-be-recorded"));
runTimeConfigurationDefault.produce(
new RunTimeConfigurationDefaultBuildItem("quarkus.mapping.rt.record-profiled", "should-not-be-recorded"));
}

public static final class Never implements BooleanSupplier {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package io.quarkus.config;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Optional;

import jakarta.inject.Inject;

import org.eclipse.microprofile.config.spi.ConfigSource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.extest.runtime.config.TestMappingRunTime;
import io.quarkus.test.QuarkusUnitTest;
import io.smallrye.config.DefaultValuesConfigSource;
import io.smallrye.config.SmallRyeConfig;

public class RecorderRuntimeConfigTest {
Expand All @@ -19,9 +27,24 @@ public class RecorderRuntimeConfigTest {

@Inject
SmallRyeConfig config;
@Inject
TestMappingRunTime mappingRunTime;

@Test
void runtimeConfig() {
assertEquals("from-application", config.getRawValue("recorded.property"));
assertEquals("from-application", config.getRawValue("recorded.profiled.property"));
assertEquals("from-application", config.getRawValue("quarkus.mapping.rt.record-profiled"));
assertTrue(mappingRunTime.recordProfiled().isPresent());
assertEquals("from-application", mappingRunTime.recordProfiled().get());

// Make sure that when we merge all the defaults, we override the non-profiled name
Optional<ConfigSource> configSource = config.getConfigSource("DefaultValuesConfigSource");
assertTrue(configSource.isPresent());
DefaultValuesConfigSource defaultValuesConfigSource = (DefaultValuesConfigSource) configSource.get();
assertNotNull(defaultValuesConfigSource.getValue("%test.recorded.profiled.property"));
assertNull(defaultValuesConfigSource.getValue("recorded.profiled.property"));
assertNotNull(defaultValuesConfigSource.getValue("%test.quarkus.mapping.rt.record-profiled"));
assertNull(defaultValuesConfigSource.getValue("quarkus.mapping.rt.record-profiled"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public interface TestMappingRunTime {
/** Record values from env test **/
Optional<String> record();

/** Record values with named profile **/
Optional<String> recordProfiled();

interface Group {
/**
* A Group value.
Expand Down
Loading