Skip to content

Commit

Permalink
Fix nested maps / groups in ConfigMappings (smallrye#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Dec 16, 2021
1 parent 9fdb499 commit 0112300
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,8 @@ private void processLazyMap(
sb.setLength(0);
sb.append(ni.getAllPreviousSegments());
String configKey = sb.toString();
Map<?, ?> map = getEnclosingMap.apply(mc, ni);
String rawMapKey = ni.getPreviousSegment();
Map<?, ?> map = getEnclosingMap.apply(mc, ni);
Converter<?> keyConv;
SmallRyeConfig config = mc.getConfig();
if (keyConvertWith != null) {
Expand Down Expand Up @@ -716,6 +716,13 @@ public ConfigMappingObject apply(final ConfigMappingContext context, final NameI
namingStrategy(enclosedGroup.getGroupType().getNamingStrategy(), enclosingGroup.getNamingStrategy()));
if (val == null) {
StringBuilder sb = context.getStringBuilder();
while (ni.hasPrevious()) {
if (!ni.previousSegmentEquals(mapKey)) {
ni.previous();
continue;
}
break;
}
sb.replace(0, sb.length(), ni.getAllPreviousSegments());
Object convertedKey = keyConverter.convert(mapKey);
((Map) ourEnclosing).put(convertedKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,15 @@ interface KeystoreConfig {
void yamlListMaps() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withMapping(MapConfig.class, "app")
.withSources(new YamlConfigSource("yaml", "app:\n" +
" config:\n" +
" - name: Bob\n" +
" foo: thing\n" +
" bar: false\n" +
" - name: Tim\n" +
" baz: stuff\n" +
" qux: 3"))
.withSources(new YamlConfigSource("yaml",
"app:\n" +
" config:\n" +
" - name: Bob\n" +
" foo: thing\n" +
" bar: false\n" +
" - name: Tim\n" +
" baz: stuff\n" +
" qux: 3"))
.build();

MapConfig mapping = config.getConfigMapping(MapConfig.class);
Expand All @@ -263,4 +264,62 @@ void yamlListMaps() {
interface MapConfig {
List<Map<String, String>> config();
}

@Test
void yamlMapGroupMap() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withMapping(Parent.class, "parent")
.withSources(new YamlConfigSource("yaml",
"parent:\n" +
" goodchildren:\n" +
" child1:\n" +
" name: John\n" +
" attributes:\n" +
" somekey: somevalue\n" +
" anotherkey: anothervalue\n" +
" child2:\n" +
" name: James\n" +
" attributes:\n" +
" something: isbroken\n" +
" badchildren:\n" +
" child3:\n" +
" name: BadJohn\n" +
" attributes:\n" +
" somekeybad: somevaluebad\n" +
" anotherkeybad: anothervaluebad "))
.build();

Parent mapping = config.getConfigMapping(Parent.class);
assertEquals(2, mapping.goodChildren().size());
assertEquals("John", mapping.goodChildren().get("child1").name());
assertEquals(2, mapping.goodChildren().get("child1").attributes().size());
assertEquals("somevalue", mapping.goodChildren().get("child1").attributes().get("somekey"));
assertEquals("anothervalue", mapping.goodChildren().get("child1").attributes().get("anotherkey"));

assertEquals("James", mapping.goodChildren().get("child2").name());
assertEquals(1, mapping.goodChildren().get("child2").attributes().size());
assertEquals("isbroken", mapping.goodChildren().get("child2").attributes().get("something"));
assertEquals(1, mapping.badChildren().size());
assertEquals("BadJohn", mapping.badChildren().get("child3").name());
assertEquals(2, mapping.badChildren().get("child3").attributes().size());
assertEquals("somevaluebad", mapping.badChildren().get("child3").attributes().get("somekeybad"));
assertEquals("anothervaluebad", mapping.badChildren().get("child3").attributes().get("anotherkeybad"));
}

@ConfigMapping(prefix = "parent")
public interface Parent {
@WithName("goodchildren")
Map<String, GrandChild> goodChildren();

@WithName("badchildren")
Map<String, GrandChild> badChildren();

interface GrandChild {
@WithName("name")
String name();

@WithName("attributes")
Map<String, String> attributes();
}
}
}

0 comments on commit 0112300

Please sign in to comment.