Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nested optionals with empty values in the middle #734

Merged
merged 1 commit into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,19 @@ private void processLazyPropertyInGroup(

if (optional && property.asOptional().getNestedProperty().isGroup()) {
GroupProperty nestedGroup = property.asOptional().getNestedProperty().asGroup();
// To recursively create Optional nested groups
BiFunction<ConfigMappingContext, NameIterator, ConfigMappingObject> delegate = new BiFunction<ConfigMappingContext, NameIterator, ConfigMappingObject>() {
@SuppressWarnings("unchecked")
@Override
public ConfigMappingObject apply(ConfigMappingContext configMappingContext, NameIterator nameIterator) {
if (matchAction instanceof BiFunction) {
return (ConfigMappingObject) ((BiFunction) matchAction).apply(configMappingContext, nameIterator);
}
return null;
}
};
GetOrCreateEnclosingGroupInGroup nestedMatchAction = new GetOrCreateEnclosingGroupInGroup(
property.isParentPropertyName() ? getEnclosingFunction
: new ConsumeOneAndThenFn<>(getEnclosingFunction),
property.isParentPropertyName() ? delegate : new ConsumeOneAndThenFn<>(delegate),
group, nestedGroup, currentPath);
processLazyGroupInGroup(currentPath, matchActions, defaultValues, namingStrategy, nestedGroup.getGroupType(),
nestedMatchAction,
Expand Down Expand Up @@ -727,10 +737,10 @@ static class GetFieldOfEnclosing implements BiFunction<ConfigMappingContext, Nam
this.memberName = memberName;
}

public ConfigMappingObject apply(final ConfigMappingContext mc, final NameIterator ni) {
ConfigMappingObject outer = getEnclosingFunction.apply(mc, ni);
public ConfigMappingObject apply(final ConfigMappingContext context, final NameIterator ni) {
ConfigMappingObject outer = getEnclosingFunction.apply(context, ni);
// eagerly populated groups will always exist
return (ConfigMappingObject) mc.getEnclosedField(type, memberName, outer);
return (ConfigMappingObject) context.getEnclosedField(type, memberName, outer);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1387,4 +1387,31 @@ void envPropertiesWithoutDottedProperties() {
assertEquals("p@ssw0rd", mapping.client().get().keystore().password());
//assertFalse(mapping.client().get().endpoints().isEmpty());
}

@ConfigMapping(prefix = "optionals")
interface NestedOptionals {
Optional<First> first();

interface First {
Optional<Second> second();

interface Second {
String value();
}
}
}

@Test
void nestedOptionals() throws Exception {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withMapping(NestedOptionals.class)
.withSources(config("optionals.first.second.value", "value"))
.build();

NestedOptionals mapping = config.getConfigMapping(NestedOptionals.class);

assertTrue(mapping.first().isPresent());
assertTrue(mapping.first().get().second().isPresent());
assertEquals("value", mapping.first().get().second().get().value());
}
}