diff --git a/core/deployment/src/main/java/io/quarkus/deployment/configuration/BuildTimeConfigurationReader.java b/core/deployment/src/main/java/io/quarkus/deployment/configuration/BuildTimeConfigurationReader.java index fb0e50785eca2d..ffc7522454a7f3 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/configuration/BuildTimeConfigurationReader.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/configuration/BuildTimeConfigurationReader.java @@ -749,9 +749,6 @@ private Set getAllProperties(final Set registeredRoots) { properties.addAll(configSource.getPropertyNames()); } } - for (String propertyName : config.getPropertyNames()) { - properties.add(propertyName); - } return properties; } diff --git a/extensions/resteasy-classic/rest-client/deployment/src/test/java/io/quarkus/restclient/configuration/RestClientBuildTimeConfigSource.java b/extensions/resteasy-classic/rest-client/deployment/src/test/java/io/quarkus/restclient/configuration/RestClientBuildTimeConfigSource.java new file mode 100644 index 00000000000000..94552c95196605 --- /dev/null +++ b/extensions/resteasy-classic/rest-client/deployment/src/test/java/io/quarkus/restclient/configuration/RestClientBuildTimeConfigSource.java @@ -0,0 +1,48 @@ +package io.quarkus.restclient.configuration; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Set; + +import org.eclipse.microprofile.config.ConfigProvider; +import org.eclipse.microprofile.config.spi.ConfigSource; + +import io.smallrye.config.common.MapBackedConfigSource; + +public class RestClientBuildTimeConfigSource extends MapBackedConfigSource { + public RestClientBuildTimeConfigSource() { + super(RestClientBuildTimeConfigSource.class.getName(), new HashMap<>()); + } + + @Override + public String getValue(final String propertyName) { + if (!propertyName.equals("io.quarkus.restclient.configuration.EchoClient/mp-rest/url")) { + return null; + } + + if (isBuildTime()) { + return "http://nohost:${quarkus.http.test-port:8081}"; + } + + return null; + } + + @Override + public Set getPropertyNames() { + return Collections.singleton("io.quarkus.restclient.configuration.EchoClient/mp-rest/url"); + } + + @Override + public int getOrdinal() { + return Integer.MAX_VALUE; + } + + private static boolean isBuildTime() { + for (ConfigSource configSource : ConfigProvider.getConfig().getConfigSources()) { + if (configSource.getClass().getSimpleName().equals("BuildTimeEnvConfigSource")) { + return true; + } + } + return false; + } +} diff --git a/extensions/resteasy-classic/rest-client/deployment/src/test/java/io/quarkus/restclient/configuration/RestClientOverrideRuntimeConfigTest.java b/extensions/resteasy-classic/rest-client/deployment/src/test/java/io/quarkus/restclient/configuration/RestClientOverrideRuntimeConfigTest.java new file mode 100644 index 00000000000000..5f7ac90cfb7777 --- /dev/null +++ b/extensions/resteasy-classic/rest-client/deployment/src/test/java/io/quarkus/restclient/configuration/RestClientOverrideRuntimeConfigTest.java @@ -0,0 +1,58 @@ +package io.quarkus.restclient.configuration; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Optional; + +import javax.inject.Inject; + +import org.eclipse.microprofile.config.spi.ConfigSource; +import org.eclipse.microprofile.rest.client.inject.RestClient; +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.test.QuarkusUnitTest; +import io.smallrye.config.ConfigValue; +import io.smallrye.config.SmallRyeConfig; + +public class RestClientOverrideRuntimeConfigTest { + @RegisterExtension + static final QuarkusUnitTest TEST = new QuarkusUnitTest().setArchiveProducer( + () -> ShrinkWrap.create(JavaArchive.class) + .addClasses(EchoResource.class, EchoClient.class, RestClientBuildTimeConfigSource.class, + RestClientRunTimeConfigSource.class) + .addAsServiceProvider("org.eclipse.microprofile.config.spi.ConfigSource", + "io.quarkus.restclient.configuration.RestClientBuildTimeConfigSource", + "io.quarkus.restclient.configuration.RestClientRunTimeConfigSource")); + + @Inject + @RestClient + EchoClient echoClient; + @Inject + SmallRyeConfig config; + + @Test + void overrideConfig() { + Optional specifiedDefaultValues = config + .getConfigSource("PropertiesConfigSource[source=Specified default values]"); + assertTrue(specifiedDefaultValues.isPresent()); + assertTrue(specifiedDefaultValues.get().getPropertyNames() + .contains("io.quarkus.restclient.configuration.EchoClient/mp-rest/url")); + // Not recorded with the build value because it comes from the interceptor and if we record it, the user needs to override with this keys instead of the old one + assertFalse(specifiedDefaultValues.get().getPropertyNames() + .contains("quarkus.rest-client.\"io.quarkus.restclient.configuration.EchoClient\".url")); + + ConfigValue mpValue = config.getConfigValue("io.quarkus.restclient.configuration.EchoClient/mp-rest/url"); + // Fallbacks from runtime to the override build time value + ConfigValue quarkusValue = config + .getConfigValue("quarkus.rest-client.\"io.quarkus.restclient.configuration.EchoClient\".url"); + assertEquals(mpValue.getValue(), quarkusValue.getValue()); + assertEquals(RestClientRunTimeConfigSource.class.getName(), quarkusValue.getConfigSourceName()); + + assertEquals("Hi", echoClient.echo("Hi")); + } +} diff --git a/extensions/resteasy-classic/rest-client/deployment/src/test/java/io/quarkus/restclient/configuration/RestClientRunTimeConfigSource.java b/extensions/resteasy-classic/rest-client/deployment/src/test/java/io/quarkus/restclient/configuration/RestClientRunTimeConfigSource.java new file mode 100644 index 00000000000000..92f01d7b5950f5 --- /dev/null +++ b/extensions/resteasy-classic/rest-client/deployment/src/test/java/io/quarkus/restclient/configuration/RestClientRunTimeConfigSource.java @@ -0,0 +1,50 @@ +package io.quarkus.restclient.configuration; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Set; + +import org.eclipse.microprofile.config.ConfigProvider; +import org.eclipse.microprofile.config.spi.ConfigSource; + +import io.quarkus.runtime.annotations.StaticInitSafe; +import io.smallrye.config.common.MapBackedConfigSource; + +@StaticInitSafe +public class RestClientRunTimeConfigSource extends MapBackedConfigSource { + public RestClientRunTimeConfigSource() { + super(RestClientRunTimeConfigSource.class.getName(), new HashMap<>()); + } + + @Override + public String getValue(final String propertyName) { + if (!propertyName.equals("io.quarkus.restclient.configuration.EchoClient/mp-rest/url")) { + return null; + } + + if (isRuntime()) { + return "http://localhost:${quarkus.http.test-port:8081}"; + } + + return null; + } + + @Override + public Set getPropertyNames() { + return Collections.singleton("io.quarkus.restclient.configuration.EchoClient/mp-rest/url"); + } + + @Override + public int getOrdinal() { + return Integer.MAX_VALUE; + } + + private static boolean isRuntime() { + for (ConfigSource configSource : ConfigProvider.getConfig().getConfigSources()) { + if (configSource.getName().equals("PropertiesConfigSource[source=Specified default values]")) { + return true; + } + } + return false; + } +}