Skip to content

Commit

Permalink
Properly fix nested Optionals and Maps when properties from the Map a…
Browse files Browse the repository at this point in the history
…re mapped first (smallrye#766)
  • Loading branch information
radcortez authored Jun 13, 2022
1 parent a6f79e2 commit a2f8e1e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,22 @@ private void processLazyMap(

addAction(currentPath, property, (mc, ni) -> {
StringBuilder sb = mc.getStringBuilder();
sb.setLength(0);
sb.append(ni.getAllPreviousSegments());
// We may need to reset the StringBuilder because the delegate may not be a Map
boolean restore = false;
if (ni.getPosition() != -1) {
restore = true;
ni.previous();
sb.setLength(0);
sb.append(ni.getAllPreviousSegments());
}
Map<?, ?> map = getEnclosingMap.apply(mc, ni);
if (restore) {
ni.next();
sb.setLength(0);
sb.append(ni.getAllPreviousSegments());
}
String configKey = sb.toString();
String rawMapKey = ni.getPreviousSegment();
Map<?, ?> map = getEnclosingMap.apply(mc, ni);
Converter<?> keyConv;
SmallRyeConfig config = mc.getConfig();
if (keyConvertWith != null) {
Expand All @@ -463,7 +474,9 @@ private void processLazyMap(
} else if (valueProperty.isMap()) {
currentPath.addLast("*");
processLazyMap(currentPath, matchActions, defaultValues, valueProperty.asMap(), (mc, ni) -> {
ni.previous();
Map<?, ?> enclosingMap = getEnclosingMap.apply(mc, ni);
ni.next();
String rawMapKey = ni.getPreviousSegment();
Converter<?> keyConv;
SmallRyeConfig config = mc.getConfig();
Expand Down Expand Up @@ -626,7 +639,9 @@ static class GetOrCreateEnclosingGroupInMap implements BiFunction<ConfigMappingC

@SuppressWarnings({ "unchecked", "rawtypes" })
public ConfigMappingObject apply(final ConfigMappingContext context, final NameIterator ni) {
ni.previous();
Map<?, ?> ourEnclosing = getEnclosingMap.apply(context, ni);
ni.next();
String mapKey = mapKey(ni);
Converter<?> keyConverter = context.getKeyConverter(enclosingGroup.getInterfaceType(),
enclosingMap.getMethod().getName(), enclosingMap.getLevels() - 1);
Expand Down Expand Up @@ -693,23 +708,7 @@ static class GetOrCreateEnclosingMapInGroup implements BiFunction<ConfigMappingC
boolean consumeName = !enclosedGroup.isParentPropertyName();
if (consumeName)
ni.previous();

// We may need to reset the StringBuilder because the delegate may not be a Map
boolean restore = false;
StringBuilder sb = context.getStringBuilder();
if (ni.getPosition() != -1) {
restore = true;
ni.previous();
sb.setLength(0);
sb.append(ni.getAllPreviousSegments());
}
ConfigMappingObject ourEnclosing = delegate.apply(context, ni);
// Restore
if (restore) {
ni.next();
sb.setLength(0);
sb.append(ni.getAllPreviousSegments());
}
if (consumeName)
ni.next();
Class<?> enclosingType = enclosingGroup.getInterfaceType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1488,4 +1488,47 @@ void nestedOptionalsGroupMap() {
assertEquals("value", mapping.first().get().second().get().third().get().properties().get("key"));
assertEquals("value", mapping.first().get().second().get().third().get().value());
}

@ConfigMapping(prefix = "optional-map")
public interface NestedOptionalMapGroup {
Optional<Boolean> enable();

Map<String, Map<String, MessageUtilConfiguration>> map();

interface MessageUtilConfiguration {
Optional<Boolean> enable();
}
}

@Test
void nestedOptionalAndMaps() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withMapping(NestedOptionalMapGroup.class)
.withSources(config("optional-map.enable", "true"))
.withSources(config("optional-map.map.filter.default.enable", "false",
"optional-map.map.filter.get-jokes-uni.enable", "true"))
.withSources(config("optional-map.map.client.reaction-api.enable", "true",
"optional-map.map.client.setup-api.enable", "true"))
.build();

NestedOptionalMapGroup mapping = config.getConfigMapping(NestedOptionalMapGroup.class);
assertTrue(mapping.enable().isPresent());
assertTrue(mapping.enable().get());

assertEquals(2, mapping.map().size());
assertTrue(mapping.map().containsKey("filter"));
assertTrue(mapping.map().get("filter").containsKey("default"));
assertTrue(mapping.map().get("filter").containsKey("get-jokes-uni"));
assertTrue(mapping.map().containsKey("client"));
assertTrue(mapping.map().get("client").containsKey("reaction-api"));
assertTrue(mapping.map().get("client").containsKey("setup-api"));
assertTrue(mapping.map().get("filter").get("default").enable().isPresent());
assertFalse(mapping.map().get("filter").get("default").enable().get());
assertTrue(mapping.map().get("filter").get("get-jokes-uni").enable().isPresent());
assertTrue(mapping.map().get("filter").get("get-jokes-uni").enable().get());
assertTrue(mapping.map().get("client").get("reaction-api").enable().isPresent());
assertTrue(mapping.map().get("client").get("reaction-api").enable().get());
assertTrue(mapping.map().get("client").get("setup-api").enable().isPresent());
assertTrue(mapping.map().get("client").get("setup-api").enable().get());
}
}

0 comments on commit a2f8e1e

Please sign in to comment.