From 98d006da4776a8d8c1e0363c5f3d4214570f7733 Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Fri, 2 Feb 2024 13:56:48 +0000 Subject: [PATCH] Do not index keys when populating a non Collection value Map (#1106) --- .../smallrye/config/ConfigMappingContext.java | 5 +---- .../io/smallrye/config/SmallRyeConfig.java | 22 +++++++++++++++---- .../smallrye/config/SmallRyeConfigTest.java | 9 +++++--- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/implementation/src/main/java/io/smallrye/config/ConfigMappingContext.java b/implementation/src/main/java/io/smallrye/config/ConfigMappingContext.java index 1af80b1ae..0a53853f8 100644 --- a/implementation/src/main/java/io/smallrye/config/ConfigMappingContext.java +++ b/implementation/src/main/java/io/smallrye/config/ConfigMappingContext.java @@ -575,10 +575,7 @@ public > ObjectCreator values( @Override public Object apply(final String propertyName) { usedProperties.add(propertyName); - for (String mapKey : config.getMapKeys(propertyName).values()) { - usedProperties.add(mapKey); - usedProperties.addAll(config.getIndexedProperties(mapKey)); - } + usedProperties.addAll(config.getMapKeys(propertyName).values()); Converter keyConverter = getConverter(keyRawType, keyConvertWith); Converter valueConverter = getConverter(valueRawType, valueConvertWith); diff --git a/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java b/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java index 8c3beafc3..f1b4791cb 100644 --- a/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java +++ b/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java @@ -247,7 +247,7 @@ public > Map getValues( return getValue(name, newMapConverter(keyConverter, newCollectionConverter(valueConverter, collectionFactory), mapFactory)); } catch (NoSuchElementException e) { - Map mapCollectionKeys = getMapKeys(name); + Map mapCollectionKeys = getMapIndexedKeys(name); if (mapCollectionKeys.isEmpty()) { throw new NoSuchElementException(ConfigMessages.msg.propertyNotFound(name)); } @@ -267,8 +267,22 @@ public Map getMapKeys(final String name) { if (propertyName.length() > name.length() + 1 && (name.isEmpty() || propertyName.charAt(name.length()) == '.') && propertyName.startsWith(name)) { - String key = unquoted(unindexed(propertyName), name.isEmpty() ? 0 : name.length() + 1); - mapKeys.put(key, unindexed(propertyName)); + String key = unquoted(propertyName, name.isEmpty() ? 0 : name.length() + 1); + mapKeys.put(key, propertyName); + } + } + return mapKeys; + } + + public Map getMapIndexedKeys(final String name) { + Map mapKeys = new HashMap<>(); + for (String propertyName : getPropertyNames()) { + if (propertyName.length() > name.length() + 1 + && (name.isEmpty() || propertyName.charAt(name.length()) == '.') + && propertyName.startsWith(name)) { + String unindexedName = unindexed(propertyName); + String key = unquoted(unindexedName, name.isEmpty() ? 0 : name.length() + 1); + mapKeys.put(key, unindexedName); } } return mapKeys; @@ -495,7 +509,7 @@ public > Optional> getOptionalValues( return optionalValue; } - Map mapKeys = getMapKeys(name); + Map mapKeys = getMapIndexedKeys(name); if (mapKeys.isEmpty()) { return Optional.empty(); } diff --git a/implementation/src/test/java/io/smallrye/config/SmallRyeConfigTest.java b/implementation/src/test/java/io/smallrye/config/SmallRyeConfigTest.java index 18c7fe7b3..9886a698e 100644 --- a/implementation/src/test/java/io/smallrye/config/SmallRyeConfigTest.java +++ b/implementation/src/test/java/io/smallrye/config/SmallRyeConfigTest.java @@ -370,14 +370,16 @@ void getValuesMap() { .withSources(config( "my.prop.key", "value", "my.prop.key.nested", "value", - "my.prop.\"key.quoted\"", "value")) + "my.prop.\"key.quoted\"", "value", + "my.prop.key.indexed[0]", "value")) .build(); Map map = config.getValues("my.prop", String.class, String.class); - assertEquals(3, map.size()); + assertEquals(4, map.size()); assertEquals("value", map.get("key")); assertEquals("value", map.get("key.nested")); assertEquals("value", map.get("key.quoted")); + assertEquals("value", map.get("key.indexed[0]")); Converter stringConverter = config.requireConverter(String.class); Map treeMap = config.getValues("my.prop", stringConverter, stringConverter, t -> new TreeMap<>()); @@ -385,10 +387,11 @@ void getValuesMap() { Optional> optionalMap = config.getOptionalValues("my.prop", String.class, String.class); assertTrue(optionalMap.isPresent()); - assertEquals(3, optionalMap.get().size()); + assertEquals(4, optionalMap.get().size()); assertEquals("value", optionalMap.get().get("key")); assertEquals("value", optionalMap.get().get("key.nested")); assertEquals("value", optionalMap.get().get("key.quoted")); + assertEquals("value", optionalMap.get().get("key.indexed[0]")); Optional> optionalTreeMap = config.getOptionalValues("my.prop", stringConverter, stringConverter, t -> new TreeMap<>());