From c7d8e63b2e4b7759111716893a90b127567ece39 Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Tue, 30 Jul 2024 16:05:43 +0100 Subject: [PATCH] Search for indexed property names before flattened comma separated value name when loading Collections for CDI injection --- .../config/inject/ConfigProducerUtil.java | 3 +-- .../IndexedPropertiesInjectionTest.java | 2 +- .../smallrye/config/SmallRyeConfigTest.java | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/cdi/src/main/java/io/smallrye/config/inject/ConfigProducerUtil.java b/cdi/src/main/java/io/smallrye/config/inject/ConfigProducerUtil.java index 9db8f3606..60eb8bd95 100644 --- a/cdi/src/main/java/io/smallrye/config/inject/ConfigProducerUtil.java +++ b/cdi/src/main/java/io/smallrye/config/inject/ConfigProducerUtil.java @@ -102,8 +102,7 @@ public static T getValue(String name, Type type, String defaultValue, Config private static T convertValues(ConfigValue configValue, Type type, SmallRyeConfig config) { List indexedProperties = config.getIndexedProperties(configValue.getName()); - // If converting a config property which exists (i.e. myProp[1] = aValue) or no indexed properties exist for the config property - if (configValue.getRawValue() != null || indexedProperties.isEmpty()) { + if (indexedProperties.isEmpty()) { return config.convertValue(configValue, resolveConverter(type, config)); } diff --git a/cdi/src/test/java/io/smallrye/config/inject/IndexedPropertiesInjectionTest.java b/cdi/src/test/java/io/smallrye/config/inject/IndexedPropertiesInjectionTest.java index 1cfa9f578..ec528ec5a 100644 --- a/cdi/src/test/java/io/smallrye/config/inject/IndexedPropertiesInjectionTest.java +++ b/cdi/src/test/java/io/smallrye/config/inject/IndexedPropertiesInjectionTest.java @@ -55,7 +55,7 @@ void indexed() { assertEquals(Stream.of(new ConvertedValue("out")).collect(Collectors.toList()), indexedBean.getConverted()); assertEquals(Stream.of("a", "b", "c").collect(Collectors.toList()), indexedBean.getDefaults()); assertEquals(Stream.of("e", "f").collect(Collectors.toList()), indexedBean.getOverrideDefaults()); - assertEquals(Stream.of("a", "b", "c").collect(Collectors.toList()), indexedBean.getComma()); + assertEquals(Stream.of("a", "b").collect(Collectors.toList()), indexedBean.getComma()); } @Test diff --git a/implementation/src/test/java/io/smallrye/config/SmallRyeConfigTest.java b/implementation/src/test/java/io/smallrye/config/SmallRyeConfigTest.java index 4894bdd71..fee700c0c 100644 --- a/implementation/src/test/java/io/smallrye/config/SmallRyeConfigTest.java +++ b/implementation/src/test/java/io/smallrye/config/SmallRyeConfigTest.java @@ -5,6 +5,7 @@ import static java.util.Collections.singletonMap; import static java.util.stream.Collectors.toSet; import static java.util.stream.StreamSupport.stream; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -107,6 +108,24 @@ void unwrap() { assertThrows(IllegalArgumentException.class, () -> config.unwrap(Object.class)); } + @Test + void getValueArray() { + SmallRyeConfig config = new SmallRyeConfigBuilder() + .withSources(config("array", "one,two,three")) + .build(); + + String[] strings = config.getValue("array", String[].class); + assertEquals(3, strings.length); + assertArrayEquals(new String[] { "one", "two", "three" }, strings); + + config = new SmallRyeConfigBuilder() + .withSources(config("array[0]", "one", "array[1]", "two", "array[2]", "three")) + .build(); + strings = config.getValue("array", String[].class); + assertEquals(3, strings.length); + assertArrayEquals(new String[] { "one", "two", "three" }, strings); + } + @Test void getIndexedValues() { SmallRyeConfig config = new SmallRyeConfigBuilder()