From 73c88e814b3cd00440d1b54df8ed21790917f83e Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Thu, 11 Jan 2024 17:58:52 +0000 Subject: [PATCH] Generate defaults for all super types (#1087) --- .../config/ConfigMappingInterface.java | 22 +++++--- .../config/ConfigMappingDefaultsTest.java | 52 +++++++++++++++++++ 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/implementation/src/main/java/io/smallrye/config/ConfigMappingInterface.java b/implementation/src/main/java/io/smallrye/config/ConfigMappingInterface.java index 4e39ce595..0aad9e244 100644 --- a/implementation/src/main/java/io/smallrye/config/ConfigMappingInterface.java +++ b/implementation/src/main/java/io/smallrye/config/ConfigMappingInterface.java @@ -95,14 +95,9 @@ public Property[] getProperties() { public Property[] getProperties(boolean includeSuper) { if (includeSuper) { - Map properties = new HashMap<>(); - for (ConfigMappingInterface superType : superTypes) { - for (Property property : superType.getProperties()) { - properties.put(property.getMethod().getName(), property); - } - } - for (Property property : getProperties()) { - properties.put(property.getMethod().getName(), property); + Map properties = getSuperProperties(this); + for (Property property : this.properties) { + properties.put(property.getMemberName(), property); } return properties.values().toArray(new Property[0]); } else { @@ -110,6 +105,17 @@ public Property[] getProperties(boolean includeSuper) { } } + private static Map getSuperProperties(ConfigMappingInterface type) { + Map properties = new HashMap<>(); + for (ConfigMappingInterface superType : type.getSuperTypes()) { + properties.putAll(getSuperProperties(superType)); + for (Property property : superType.getProperties()) { + properties.put(property.getMemberName(), property); + } + } + return properties; + } + public boolean hasNamingStrategy() { return interfaceType.getAnnotation(ConfigMapping.class) != null; } diff --git a/implementation/src/test/java/io/smallrye/config/ConfigMappingDefaultsTest.java b/implementation/src/test/java/io/smallrye/config/ConfigMappingDefaultsTest.java index cd32cc854..bd5396503 100644 --- a/implementation/src/test/java/io/smallrye/config/ConfigMappingDefaultsTest.java +++ b/implementation/src/test/java/io/smallrye/config/ConfigMappingDefaultsTest.java @@ -673,4 +673,56 @@ public interface DevServicesBuildTimeConfig { Map containerProperties(); } + + @Test + void multipleLevelDefaults() { + Map defaults = getDefaults(configClassWithPrefix(GrandChild.class)); + assertEquals("parent", defaults.get("parent")); + assertEquals("child", defaults.get("child")); + assertEquals("grand-child", defaults.get("grand-child")); + assertEquals("child", defaults.get("override-by-child")); + assertEquals("grand-child", defaults.get("override-by-grand-child")); + + SmallRyeConfig config = new SmallRyeConfigBuilder() + .withMapping(GrandChild.class) + .build(); + + GrandChild mapping = config.getConfigMapping(GrandChild.class); + assertEquals("parent", mapping.parent()); + assertEquals("child", mapping.child()); + assertEquals("grand-child", mapping.grandChild()); + assertEquals("child", mapping.overrideByChild()); + assertEquals("grand-child", mapping.overrideByGrandChild()); + } + + interface Parent { + @WithDefault("parent") + String parent(); + + @WithDefault("parent") + String overrideByChild(); + + @WithDefault("parent") + String overrideByGrandChild(); + } + + interface Child extends Parent { + @WithDefault("child") + String child(); + + @WithDefault("child") + String overrideByChild(); + + @WithDefault("child") + String overrideByGrandChild(); + } + + @ConfigMapping + interface GrandChild extends Child { + @WithDefault("grand-child") + String grandChild(); + + @WithDefault("grand-child") + String overrideByGrandChild(); + } }