From 30f478c52a0cc49c7899c013f74d69ee0fc06424 Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Thu, 29 Aug 2024 14:07:03 +0100 Subject: [PATCH] Avoid expensive exception and log when getValues fails lookup for indexed properties and fallbacks to comma (#1216) --- .../java/io/smallrye/config/SmallRyeConfig.java | 15 +++++++++------ .../io/smallrye/config/SmallRyeConfigTest.java | 6 +++++- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java b/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java index 8db2ec4c7..a55fbfa86 100644 --- a/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java +++ b/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java @@ -184,11 +184,11 @@ public > C getValues(String name, Class itemClass, } public > C getValues(String name, Converter converter, IntFunction collectionFactory) { - try { - return getIndexedValues(name, converter, collectionFactory); - } catch (NoSuchElementException e) { - return getValue(name, newCollectionConverter(converter, collectionFactory)); + List indexedProperties = getIndexedProperties(name); + if (!indexedProperties.isEmpty()) { + return getIndexedValues(indexedProperties, converter, collectionFactory); } + return getValue(name, newCollectionConverter(converter, collectionFactory)); } public > C getIndexedValues(String name, Converter converter, @@ -197,12 +197,15 @@ public > C getIndexedValues(String name, Converter if (indexedProperties.isEmpty()) { throw new NoSuchElementException(ConfigMessages.msg.propertyNotFound(name)); } + return getIndexedValues(indexedProperties, converter, collectionFactory); + } - final C collection = collectionFactory.apply(indexedProperties.size()); + private > C getIndexedValues(List indexedProperties, Converter converter, + IntFunction collectionFactory) { + C collection = collectionFactory.apply(indexedProperties.size()); for (String indexedProperty : indexedProperties) { collection.add(getValue(indexedProperty, converter)); } - return collection; } diff --git a/implementation/src/test/java/io/smallrye/config/SmallRyeConfigTest.java b/implementation/src/test/java/io/smallrye/config/SmallRyeConfigTest.java index fee700c0c..4bea991d8 100644 --- a/implementation/src/test/java/io/smallrye/config/SmallRyeConfigTest.java +++ b/implementation/src/test/java/io/smallrye/config/SmallRyeConfigTest.java @@ -134,7 +134,8 @@ void getIndexedValues() { "server.environments[2]", "prod")) .build(); - List environments = config.getValues("server.environments", String.class); + List environments = config.getIndexedValues("server.environments", config.requireConverter(String.class), + ArrayList::new); assertEquals(3, environments.size()); assertEquals("dev", environments.get(0)); assertEquals("dev", config.getValue("server.environments[0]", String.class)); @@ -142,6 +143,9 @@ void getIndexedValues() { assertEquals("qa", config.getValue("server.environments[1]", String.class)); assertEquals("prod", environments.get(2)); assertEquals("prod", config.getValue("server.environments[2]", String.class)); + + assertThrows(NoSuchElementException.class, + () -> config.getIndexedValues("not.found", config.requireConverter(String.class), ArrayList::new)); } @Test