From 444689c9d65ff17319025415642472963eba6ada Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Fri, 31 Mar 2023 17:10:02 +0100 Subject: [PATCH] Support property names in KeyMapBackedConfigSource (#919) --- .../main/java/io/smallrye/config/KeyMap.java | 17 +++++++++++ .../config/KeyMapBackedConfigSource.java | 2 +- .../config/SmallRyeConfigBuilder.java | 1 - .../config/ConfigMappingInterfaceTest.java | 29 +++++++++++++++++-- .../java/io/smallrye/config/KeyMapTest.java | 16 ++++++++++ 5 files changed, 61 insertions(+), 4 deletions(-) diff --git a/implementation/src/main/java/io/smallrye/config/KeyMap.java b/implementation/src/main/java/io/smallrye/config/KeyMap.java index 5d1ea994a..6314e222f 100644 --- a/implementation/src/main/java/io/smallrye/config/KeyMap.java +++ b/implementation/src/main/java/io/smallrye/config/KeyMap.java @@ -2,6 +2,7 @@ import java.io.Serializable; import java.util.HashMap; +import java.util.HashSet; import java.util.IdentityHashMap; import java.util.Iterator; import java.util.Map; @@ -310,6 +311,22 @@ public void putAll(final Map> m) { } } + @Override + public Set keySet() { + Set keys = super.keySet(); + Set allKeys = new HashSet<>(); + for (String key : keys) { + Set childKeys = find(key).keySet(); + for (String childKey : childKeys) { + allKeys.add(key + "." + childKey); + } + if (hasRootValue(key)) { + allKeys.add(key); + } + } + return allKeys; + } + @SuppressWarnings("ResultOfMethodCallIgnored") public StringBuilder toString(StringBuilder b) { b.append("KeyMap("); diff --git a/implementation/src/main/java/io/smallrye/config/KeyMapBackedConfigSource.java b/implementation/src/main/java/io/smallrye/config/KeyMapBackedConfigSource.java index 08e644444..fb4791f76 100644 --- a/implementation/src/main/java/io/smallrye/config/KeyMapBackedConfigSource.java +++ b/implementation/src/main/java/io/smallrye/config/KeyMapBackedConfigSource.java @@ -34,7 +34,7 @@ KeyMap getKeyMapProperties() { @Override public Set getPropertyNames() { - return Collections.emptySet(); + return properties.keySet(); } @Override diff --git a/implementation/src/main/java/io/smallrye/config/SmallRyeConfigBuilder.java b/implementation/src/main/java/io/smallrye/config/SmallRyeConfigBuilder.java index 8b8cc28f0..c0ad807d6 100644 --- a/implementation/src/main/java/io/smallrye/config/SmallRyeConfigBuilder.java +++ b/implementation/src/main/java/io/smallrye/config/SmallRyeConfigBuilder.java @@ -317,7 +317,6 @@ public OptionalInt getPriority() { } })); - withDefaultValue("smallrye.config.secret-handlers", "all"); interceptors.add(new InterceptorWithPriority(new ConfigSourceInterceptorFactory() { @Override public ConfigSourceInterceptor getInterceptor(final ConfigSourceInterceptorContext context) { diff --git a/implementation/src/test/java/io/smallrye/config/ConfigMappingInterfaceTest.java b/implementation/src/test/java/io/smallrye/config/ConfigMappingInterfaceTest.java index 74b5bd042..7e175b7bf 100644 --- a/implementation/src/test/java/io/smallrye/config/ConfigMappingInterfaceTest.java +++ b/implementation/src/test/java/io/smallrye/config/ConfigMappingInterfaceTest.java @@ -4,6 +4,7 @@ import static io.smallrye.config.ConfigMappingInterfaceTest.MapKeyEnum.ClientId.NAF; import static io.smallrye.config.ConfigMappingInterfaceTest.MapKeyEnum.ClientId.SOS_DAH; import static io.smallrye.config.KeyValuesConfigSource.config; +import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_MAPPING_VALIDATE_UNKNOWN; import static java.util.Collections.singletonList; import static java.util.stream.Collectors.toList; import static java.util.stream.StreamSupport.stream; @@ -356,8 +357,9 @@ void defaults() { assertEquals("bar", defaults.bar()); assertEquals("foo", config.getRawValue("foo")); - final List propertyNames = stream(config.getPropertyNames().spliterator(), false).collect(toList()); - assertFalse(propertyNames.contains("foo")); + List propertyNames = stream(config.getPropertyNames().spliterator(), false).collect(toList()); + assertTrue(propertyNames.contains("foo")); + assertTrue(propertyNames.contains("bar")); } @Test @@ -1619,6 +1621,7 @@ void defaultsBuilderAndMapping() { .addDefaultInterceptors() .withMapping(DefaultsBuilderAndMapping.class) .withDefaultValue("server.host", "localhost") + .withDefaultValue(SMALLRYE_CONFIG_MAPPING_VALIDATE_UNKNOWN, "false") .build(); DefaultsBuilderAndMapping mapping = config.getConfigMapping(DefaultsBuilderAndMapping.class); @@ -1901,4 +1904,26 @@ interface MapKeys { Map> list(); } + + @Test + void defaultsPropertyNames() { + SmallRyeConfig config = new SmallRyeConfigBuilder() + .addDefaultInterceptors() + .withMapping(DefaultsPropertyNames.class) + .build(); + + DefaultsPropertyNames mapping = config.getConfigMapping(DefaultsPropertyNames.class); + assertEquals("value", mapping.value()); + assertEquals("value", config.getRawValue("defaults.myProperty")); + + Set properties = stream(config.getPropertyNames().spliterator(), false).collect(Collectors.toSet()); + assertTrue(properties.contains("defaults.myProperty")); + } + + @ConfigMapping(prefix = "defaults") + interface DefaultsPropertyNames { + @WithDefault("value") + @WithName("myProperty") + String value(); + } } diff --git a/implementation/src/test/java/io/smallrye/config/KeyMapTest.java b/implementation/src/test/java/io/smallrye/config/KeyMapTest.java index 379a5d06b..f66e0e906 100644 --- a/implementation/src/test/java/io/smallrye/config/KeyMapTest.java +++ b/implementation/src/test/java/io/smallrye/config/KeyMapTest.java @@ -3,10 +3,12 @@ import static java.util.stream.Collectors.toList; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.ArrayDeque; import java.util.HashMap; import java.util.Map; +import java.util.Set; import java.util.stream.Stream; import org.junit.jupiter.api.Test; @@ -280,4 +282,18 @@ void star() { assertEquals("value", map.findRootValue("map.key.one.two")); assertEquals("value", map.findRootValue("map.key.one.two.three")); } + + @Test + void keySet() { + KeyMap map = new KeyMap<>(); + map.findOrAdd("root.foo").putRootValue("bar"); + map.findOrAdd("root.foo.bar").putRootValue("baz"); + map.findOrAdd("root.foo.bar.*").putRootValue("baz"); + map.findOrAdd("root.foo.bar.*.baz").putRootValue("anything"); + + Set keys = map.keySet(); + assertEquals(2, keys.size()); + assertTrue(keys.contains("root.foo")); + assertTrue(keys.contains("root.foo.bar")); + } }