Skip to content

Commit 63aaafa

Browse files
Filter non stork configs and read them as optional values (#1204)
Refactor improvements Co-authored-by: Clement Escoffier <clement@apache.org>
1 parent 04027e7 commit 63aaafa

4 files changed

Lines changed: 67 additions & 6 deletions

File tree

microprofile/src/main/java/io/smallrye/stork/microprofile/MicroProfileConfigProvider.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.slf4j.Logger;
1010
import org.slf4j.LoggerFactory;
1111

12+
import io.smallrye.stork.Stork;
1213
import io.smallrye.stork.api.config.ServiceConfig;
1314
import io.smallrye.stork.spi.config.ConfigProvider;
1415
import io.smallrye.stork.spi.config.SimpleServiceConfig;
@@ -32,8 +33,15 @@ public MicroProfileConfigProvider() {
3233
Map<String, Map<String, String>> propertiesByServiceName = new HashMap<>();
3334

3435
for (String propertyName : config.getPropertyNames()) {
35-
StorkConfigUtils.computeServiceProperty(propertiesByServiceName, propertyName,
36-
config.getValue(propertyName, String.class));
36+
if (propertyName.startsWith(Stork.STORK + ".")) {
37+
config.getOptionalValue(propertyName, String.class)
38+
.map(String::trim)
39+
.filter(value -> !value.isEmpty())
40+
.ifPresentOrElse(
41+
value -> StorkConfigUtils.computeServiceProperty(propertiesByServiceName,
42+
propertyName, value),
43+
() -> log.debug("Ignoring empty stork config property: {}", propertyName));
44+
}
3745
}
3846

3947
for (Map.Entry<String, Map<String, String>> serviceEntry : propertiesByServiceName.entrySet()) {

microprofile/src/test/java/io/smallrye/stork/microprofile/MicroProfileConfigProviderTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,28 @@ void shouldHandleServiceNamesInQuotes() {
233233
assertThat(lb.getType()).isEqualTo("test-lb-2");
234234
}
235235

236+
@Test
237+
void shouldFilterNonStorkPropertiesAndIgnoreEmptyValues() {
238+
Map<String, String> properties = new HashMap<>();
239+
properties.put("stork." + FIRST_SERVICE + ".service-discovery", "test-sd-1");
240+
properties.put("stork." + FIRST_SERVICE + ".service-discovery.one", "http://localhost:8080");
241+
properties.put("stork." + FIRST_SERVICE + ".service-discovery.two", "");
242+
properties.put("stork." + FIRST_SERVICE + ".service-discovery.three", " ");
243+
properties.put("user.script", "");
244+
properties.put("some.other.property", "");
245+
properties.put("non.stork.property", "some-value");
246+
247+
Stork stork = storkForConfig(properties);
248+
249+
ServiceDiscovery serviceDiscovery = stork.getService(FIRST_SERVICE).getServiceDiscovery();
250+
assertThat(serviceDiscovery).isNotNull().isInstanceOf(TestServiceDiscovery.class);
251+
252+
TestServiceDiscovery sd = (TestServiceDiscovery) serviceDiscovery;
253+
assertThat(sd.getType()).isEqualTo("test-sd-1");
254+
assertThat(sd.getConfig().getOne()).isEqualTo("http://localhost:8080");
255+
assertThat(sd.getConfig().getTwo()).isNull();
256+
}
257+
236258
private Stork storkForConfig(Map<String, String> properties) {
237259
Map<String, ConfigValue> backend = new HashMap<>();
238260
for (Map.Entry<String, String> entry : properties.entrySet()) {

spring-boot/src/main/java/io/smallrye/stork/springboot/SpringBootConfigProvider.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@
55
import java.util.List;
66
import java.util.Map;
77

8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
810
import org.springframework.context.ApplicationContext;
911
import org.springframework.core.env.ConfigurableEnvironment;
1012
import org.springframework.core.env.EnumerablePropertySource;
1113
import org.springframework.core.env.PropertySource;
1214

15+
import io.smallrye.stork.Stork;
1316
import io.smallrye.stork.api.config.ServiceConfig;
1417
import io.smallrye.stork.spi.config.ConfigProvider;
1518
import io.smallrye.stork.spi.config.SimpleServiceConfig;
1619
import io.smallrye.stork.utils.StorkConfigUtils;
1720

1821
public class SpringBootConfigProvider implements ConfigProvider {
1922

23+
private static final Logger log = LoggerFactory.getLogger(SpringBootConfigProvider.class);
24+
2025
private final List<ServiceConfig> serviceConfigs = new ArrayList<>();
2126

2227
public SpringBootConfigProvider() {
@@ -26,10 +31,14 @@ public SpringBootConfigProvider() {
2631
Map<String, Map<String, String>> propertiesByServiceName = new HashMap<>();
2732

2833
for (String propertyName : getPropertyNames(environment)) {
29-
30-
StorkConfigUtils.computeServiceProperty(propertiesByServiceName, propertyName,
31-
environment.getProperty(propertyName));
32-
34+
if (propertyName.startsWith(Stork.STORK + ".")) {
35+
String value = environment.getProperty(propertyName);
36+
if (value != null && !value.trim().isEmpty()) {
37+
StorkConfigUtils.computeServiceProperty(propertiesByServiceName, propertyName, value.trim());
38+
} else {
39+
log.debug("Ignoring empty stork config property: {}", propertyName);
40+
}
41+
}
3342
}
3443

3544
for (Map.Entry<String, Map<String, String>> serviceEntry : propertiesByServiceName.entrySet()) {

spring-boot/src/test/java/io/smallrye/stork/microprofile/SpringBootConfigProviderTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,28 @@ void shouldHandleServiceNamesInQuotes() {
237237
assertThat(lb.getType()).isEqualTo("test-lb-2");
238238
}
239239

240+
@Test
241+
void shouldFilterNonStorkPropertiesAndIgnoreEmptyValues() {
242+
Map<String, String> properties = new HashMap<>();
243+
properties.put("stork." + FIRST_SERVICE + ".service-discovery", "test-sd-1");
244+
properties.put("stork." + FIRST_SERVICE + ".service-discovery.one", "http://localhost:8080");
245+
properties.put("stork." + FIRST_SERVICE + ".service-discovery.two", "");
246+
properties.put("stork." + FIRST_SERVICE + ".service-discovery.three", " ");
247+
properties.put("user.script", "");
248+
properties.put("some.other.property", "");
249+
properties.put("non.stork.property", "some-value");
250+
251+
Stork stork = storkForConfig(properties);
252+
253+
ServiceDiscovery serviceDiscovery = stork.getService(FIRST_SERVICE).getServiceDiscovery();
254+
assertThat(serviceDiscovery).isNotNull().isInstanceOf(TestServiceDiscovery.class);
255+
256+
TestServiceDiscovery sd = (TestServiceDiscovery) serviceDiscovery;
257+
assertThat(sd.getType()).isEqualTo("test-sd-1");
258+
assertThat(sd.getConfig().getOne()).isEqualTo("http://localhost:8080");
259+
assertThat(sd.getConfig().getTwo()).isNull();
260+
}
261+
240262
private Stork storkForConfig(Map<String, String> props) {
241263

242264
Properties properties = new Properties();

0 commit comments

Comments
 (0)