Skip to content

Commit

Permalink
Do not fail with inline map value in mapping (#1135)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Mar 18, 2024
1 parent cd0c4b9 commit daaa5b8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
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

0 comments on commit daaa5b8

Please sign in to comment.