Skip to content

Commit

Permalink
Support @WithConverter in the Collection type (#948)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Jun 16, 2023
1 parent dbc3675 commit 936699c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -822,8 +822,12 @@ private static Property getPropertyDef(Method method, AnnotatedType type) {
return new CollectionProperty(rawType, new GroupProperty(method, propertyName, configurationInterface));
}

Class<? extends Converter<?>> converter = getConverter(elementType, method);
if (converter != null) {
convertWith = converter;
}
return new CollectionProperty(rawType,
new LeafProperty(method, propertyName, elementType.getType(), null, getDefaultValue(method)));
new LeafProperty(method, propertyName, elementType.getType(), convertWith, getDefaultValue(method)));
}
ConfigMappingInterface configurationInterface = getConfigurationInterface(rawType);
if (configurationInterface != null) {
Expand All @@ -835,9 +839,13 @@ private static Property getPropertyDef(Method method, AnnotatedType type) {

String defaultValue = getDefaultValue(method);
if (rawType == List.class || rawType == Set.class) {
Type elementType = typeOfParameter(type.getType(), 0);
AnnotatedType elementType = typeOfParameter(type, 0);
Class<? extends Converter<?>> converter = getConverter(elementType, method);
if (converter != null) {
convertWith = converter;
}
return new CollectionProperty(rawType,
new LeafProperty(method, propertyName, elementType, convertWith, defaultValue));
new LeafProperty(method, propertyName, elementType.getType(), convertWith, defaultValue));
} else if (rawType == Optional.class) {
return new OptionalProperty(method, propertyName,
new LeafProperty(method, propertyName, type.getType(), convertWith, defaultValue));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2252,4 +2252,38 @@ interface EmptyDefault {
@WithDefault("")
String empty();
}

@Test
void withConverterListElement() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.addDefaultInterceptors()
.withMapping(WithConverterListElement.class)
.withSources(config(
"converter.list", "one, two",
"converter.elements", "one, two"))
.build();

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

assertTrue(mapping.list().isPresent());
assertEquals("one", mapping.list().get().get(0));
assertEquals("two", mapping.list().get().get(1));
assertTrue(mapping.elements().isPresent());
assertEquals("one", mapping.elements().get().get(0));
assertEquals("two", mapping.elements().get().get(1));
}

@ConfigMapping(prefix = "converter")
interface WithConverterListElement {
Optional<@WithConverter(TrimConverter.class) List<String>> list();

Optional<List<@WithConverter(TrimConverter.class) String>> elements();
}

public static class TrimConverter implements Converter<String> {
@Override
public String convert(final String value) throws IllegalArgumentException, NullPointerException {
return value.trim();
}
}
}

0 comments on commit 936699c

Please sign in to comment.