Skip to content

Commit

Permalink
Only record config properties coming from ConfigSources
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Oct 7, 2021
1 parent c55782f commit 868d740
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -749,9 +749,6 @@ private Set<String> getAllProperties(final Set<String> registeredRoots) {
properties.addAll(configSource.getPropertyNames());
}
}
for (String propertyName : config.getPropertyNames()) {
properties.add(propertyName);
}
return properties;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<ConfigSource> 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"));
}
}
Original file line number Diff line number Diff line change
@@ -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<String> 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;
}
}

0 comments on commit 868d740

Please sign in to comment.