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

Do not fail with inline map value in mapping #1135

Merged
merged 1 commit into from
Mar 18, 2024
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 @@ -2,6 +2,7 @@

import static io.smallrye.config.ConfigValidationException.Problem;
import static io.smallrye.config.common.utils.StringUtil.unindexed;
import static java.util.Collections.EMPTY_MAP;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;
Expand All @@ -12,6 +13,7 @@
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
Expand Down Expand Up @@ -591,17 +593,25 @@ public Object apply(final String propertyName) {

if (defaultValue == null) {
// TODO - We should use getValues here, but this makes the Map to be required. This is a breaking change
return config.getOptionalValues(propertyName, keyConverter, valueConverter, HashMap::new)
.orElse(new HashMap<>());
try {
return config.getOptionalValues(propertyName, keyConverter, valueConverter, HashMap::new)
.orElse(EMPTY_MAP);
} catch (NoSuchElementException e) { // Can be thrown by MapConverter, but mappings shouldn't use inline map values
return EMPTY_MAP;
}
} else {
IntFunction<Map<K, V>> mapFactory = new IntFunction<>() {
@Override
public Map<K, V> apply(final int value) {
return new MapWithDefault<>(valueConverter.convert(defaultValue));
}
};
return config.getOptionalValues(propertyName, keyConverter, valueConverter, mapFactory)
.orElse(mapFactory.apply(0));
try {
return config.getOptionalValues(propertyName, keyConverter, valueConverter, mapFactory)
.orElse(mapFactory.apply(0));
} catch (NoSuchElementException e) { // Can be thrown by MapConverter, but mappings shouldn't use inline map values
return mapFactory.apply(0);
}
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2573,14 +2573,27 @@ interface IgnorePathsRecursive {
@Test
void nestedLeafsMaps() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(config("maps.one.two", "value"))
.withMapping(NestedLeadfsMaps.class)
.build();
NestedLeadfsMaps mapping = config.getConfigMapping(NestedLeadfsMaps.class);
assertEquals("value", mapping.doubleMap().get("one").get("two"));

config = new SmallRyeConfigBuilder()
.withSources(config(
"maps.one.two", "value",
"maps.one.two.three", "value"))
.withMapping(NestedLeadfsMaps.class)
.build();
mapping = config.getConfigMapping(NestedLeadfsMaps.class);
assertEquals("value", mapping.tripleMap().get("one").get("two").get("three"));

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

config = new SmallRyeConfigBuilder()
.withSources(config(
"maps.one.two", "value",
"maps.one.two.three", "value"))
.withMapping(NestedLeadfsMaps.class)
.build();
mapping = config.getConfigMapping(NestedLeadfsMaps.class);
assertEquals("value", mapping.doubleMap().get("one").get("two"));
assertEquals("value", mapping.tripleMap().get("one").get("two").get("three"));
}
Expand Down