From 141e45232b061f3eec44fd9c934c90aa9e0649e8 Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Tue, 3 Dec 2024 14:18:33 +0000 Subject: [PATCH] Remove deprecated ConfigMappings#mappedProperties --- .../io/smallrye/config/ConfigMappings.java | 15 ----- .../smallrye/config/ConfigMappingsTest.java | 20 ------- .../RelocateConfigSourceInterceptorTest.java | 57 ++++++++++++------- 3 files changed, 37 insertions(+), 55 deletions(-) diff --git a/implementation/src/main/java/io/smallrye/config/ConfigMappings.java b/implementation/src/main/java/io/smallrye/config/ConfigMappings.java index c463298a7..aab27079d 100644 --- a/implementation/src/main/java/io/smallrye/config/ConfigMappings.java +++ b/implementation/src/main/java/io/smallrye/config/ConfigMappings.java @@ -3,7 +3,6 @@ import static io.smallrye.config.ConfigMappings.ConfigClass.configClass; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; import java.util.Objects; import java.util.Set; @@ -75,20 +74,6 @@ public static Map getProperties(final ConfigClass configClass) return properties; } - @Deprecated - public static Set mappedProperties(final ConfigClass configClass, final Set properties) { - ConfigMappingNames names = new ConfigMappingNames( - ConfigMappingLoader.getConfigMapping(configClass.getKlass()).getNames()); - Set mappedNames = new HashSet<>(); - for (String property : properties) { - if (names.hasAnyName(configClass.getKlass().getName(), configClass.getPrefix(), configClass.getPrefix(), - Set.of(property))) { - mappedNames.add(property); - } - } - return mappedNames; - } - private static void mapConfiguration( final SmallRyeConfig config, final SmallRyeConfigBuilder configBuilder, diff --git a/implementation/src/test/java/io/smallrye/config/ConfigMappingsTest.java b/implementation/src/test/java/io/smallrye/config/ConfigMappingsTest.java index bf664bca2..a53e37db5 100644 --- a/implementation/src/test/java/io/smallrye/config/ConfigMappingsTest.java +++ b/implementation/src/test/java/io/smallrye/config/ConfigMappingsTest.java @@ -4,7 +4,6 @@ import static io.smallrye.config.ConfigMappings.registerConfigProperties; import static io.smallrye.config.ConfigMappings.ConfigClass.configClass; import static io.smallrye.config.KeyValuesConfigSource.config; -import static java.util.Collections.emptySet; import static java.util.Collections.singleton; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -15,7 +14,6 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; -import java.util.Set; import org.eclipse.microprofile.config.inject.ConfigProperties; import org.eclipse.microprofile.config.spi.Converter; @@ -326,22 +324,4 @@ void properties() { assertTrue(properties.containsKey("mapped.value")); assertTrue(properties.containsKey("mapped.collection[*].value")); } - - @Test - void mappedProperties() { - Set mappedProperties = ConfigMappings.mappedProperties(configClass(MappedProperties.class), - Set.of("mapped.value", "mapped.nested.value", "mapped.collection[0].value", "mapped.unknown")); - assertEquals(3, mappedProperties.size()); - assertTrue(mappedProperties.contains("mapped.value")); - assertTrue(mappedProperties.contains("mapped.nested.value")); - assertTrue(mappedProperties.contains("mapped.collection[0].value")); - - assertTrue(ConfigMappings.mappedProperties(configClass(MappedProperties.class), emptySet()).isEmpty()); - } - - @Test - void invalidMappedProperties() { - assertTrue(ConfigMappings.mappedProperties(configClass(MappedProperties.class), - Set.of("foo.bar", "mapped", "mapped.something", "mapped.collection")).isEmpty()); - } } diff --git a/implementation/src/test/java/io/smallrye/config/RelocateConfigSourceInterceptorTest.java b/implementation/src/test/java/io/smallrye/config/RelocateConfigSourceInterceptorTest.java index 2e0c95c4d..03589f17f 100644 --- a/implementation/src/test/java/io/smallrye/config/RelocateConfigSourceInterceptorTest.java +++ b/implementation/src/test/java/io/smallrye/config/RelocateConfigSourceInterceptorTest.java @@ -1,7 +1,5 @@ package io.smallrye.config; -import static io.smallrye.config.ConfigMappings.mappedProperties; -import static io.smallrye.config.ConfigMappings.ConfigClass.configClass; import static io.smallrye.config.KeyValuesConfigSource.config; import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_PROFILE; import static java.util.Collections.singletonMap; @@ -15,7 +13,6 @@ import java.util.Collections; import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Optional; @@ -201,22 +198,22 @@ void fallbackMaps() { if (name.startsWith("child.")) { return "parent." + name.substring(6); } + if (name.startsWith("parent.")) { + return "child." + name.substring(7); + } + return name; + }); + + // To rewrite the fallback names to the main name. Required for fallbacks to work properly with Maps keys + RelocateConfigSourceInterceptor relocateConfigSourceInterceptor = new RelocateConfigSourceInterceptor(name -> { + if (name.startsWith("parent.")) { + return "child." + name.substring(7); + } return name; }) { @Override - public Iterator iterateNames(final ConfigSourceInterceptorContext context) { - Set names = new HashSet<>(); - Set hierarchyCandidates = new HashSet<>(); - Iterator namesIterator = context.iterateNames(); - while (namesIterator.hasNext()) { - String name = namesIterator.next(); - names.add(name); - if (name.startsWith("parent.")) { - hierarchyCandidates.add("child." + name.substring(7)); - } - } - names.addAll(mappedProperties(configClass(Child.class), hierarchyCandidates)); - return names.iterator(); + public ConfigValue getValue(final ConfigSourceInterceptorContext context, final String name) { + return context.proceed(name); } }; @@ -227,8 +224,11 @@ public Iterator iterateNames(final ConfigSourceInterceptorContext contex "parent.labels.parent", "parent", "parent.nested.parent.value", "parent-nested")) .withInterceptors(fallbackConfigSourceInterceptor) + .withInterceptors(relocateConfigSourceInterceptor) .withMapping(Parent.class) .withMapping(Child.class) + .withMappingIgnore("parent.**") + .withMappingIgnore("child.**") .build(); Parent parent = config.getConfigMapping(Parent.class); @@ -248,8 +248,11 @@ public Iterator iterateNames(final ConfigSourceInterceptorContext contex "parent.labels.parent", "parent", "child.labels.child", "child", "parent.nested.parent.value", "parent-nested", "child.nested.child.value", "child-nested")) .withInterceptors(fallbackConfigSourceInterceptor) + .withInterceptors(relocateConfigSourceInterceptor) .withMapping(Parent.class) .withMapping(Child.class) + .withMappingIgnore("parent.**") + .withMappingIgnore("child.**") .build(); parent = config.getConfigMapping(Parent.class); @@ -257,11 +260,11 @@ public Iterator iterateNames(final ConfigSourceInterceptorContext contex assertEquals("parent", parent.value().orElse(null)); assertEquals("child", child.value().orElse(null)); - assertEquals(1, parent.labels().size()); + assertEquals(2, parent.labels().size()); assertEquals("parent", parent.labels().get("parent")); assertEquals(2, child.labels().size()); assertEquals("child", child.labels().get("child")); - assertEquals(1, parent.nested().size()); + assertEquals(2, parent.nested().size()); assertEquals("parent-nested", parent.nested().get("parent").value()); assertEquals(2, child.nested().size()); assertEquals("child-nested", child.nested().get("child").value()); @@ -326,7 +329,14 @@ void relocateMapping() { assertEquals("old", mapping.map().get("old")); Set properties = stream(config.getPropertyNames().spliterator(), false).collect(toSet()); - Set mappedProperties = mappedProperties(configClass(RelocateMapping.class), properties); + ConfigMappingNames names = new ConfigMappingNames( + ConfigMappingLoader.getConfigMapping(RelocateMapping.class).getNames()); + Set mappedProperties = new HashSet<>(); + for (String property : properties) { + if (names.hasAnyName(RelocateMapping.class.getName(), "reloc.old", "reloc.old", Set.of(property))) { + mappedProperties.add(property); + } + } properties.removeAll(mappedProperties); Set relocateProperties = new HashSet<>(); for (String property : properties) { @@ -362,7 +372,14 @@ void fallbackMapping() { assertEquals("old", mapping.map().get("old")); Set properties = stream(config.getPropertyNames().spliterator(), false).collect(toSet()); - Set mappedProperties = mappedProperties(configClass(FallbackMapping.class), properties); + ConfigMappingNames names = new ConfigMappingNames( + ConfigMappingLoader.getConfigMapping(FallbackMapping.class).getNames()); + Set mappedProperties = new HashSet<>(); + for (String property : properties) { + if (names.hasAnyName(FallbackMapping.class.getName(), "fall.new", "fall.new", Set.of(property))) { + mappedProperties.add(property); + } + } properties.removeAll(mappedProperties); Set fallbackProperties = new HashSet<>(); for (String property : properties) {