This is an automated email from the ASF dual-hosted git repository. jdaugherty pushed a commit to branch 7.0.x in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit bd674957eb81e7145f77646a39ff44b7e0b75318 Author: James Daugherty <[email protected]> AuthorDate: Fri Oct 16 12:58:15 2026 -0400 Resolve every CompositeConfig view from the config that has the key A CompositeConfig answers getProperty from the highest precedence config that has the key, but its Map views did not agree. size(), values() and entrySet() summed the delegates, so a key shared by two configs was counted and listed twice, and flatten() and toProperties() let the lowest precedence config overwrite the others, the opposite of the lookup. get() and getAt() stopped at the first config that answered at all, and a config answers a key it does not have with a navigator, so a key only a lower config has came back as an empty navigator instead of its value. Every view now derives from one rule: a key is resolved by the highest precedence config that contains it. keySet() is the ordered union of the delegates' keys, size() is its size, values() and entrySet() resolve each key once, containsValue() looks at those resolved values, and flatten() and toProperties() merge the delegates from lowest to highest precedence so the highest wins. When no config has the key, get() and getAt() fall back to the first non-null answer as before, which keeps nested navigation working. --- .../groovy/org/grails/config/CompositeConfig.java | 59 +++++++++++++--------- .../org/grails/config/CompositeConfigSpec.groovy | 40 +++++++++++---- 2 files changed, 66 insertions(+), 33 deletions(-) diff --git a/grails-core/src/main/groovy/org/grails/config/CompositeConfig.java b/grails-core/src/main/groovy/org/grails/config/CompositeConfig.java index 91fa679258..03eff1b11c 100644 --- a/grails-core/src/main/groovy/org/grails/config/CompositeConfig.java +++ b/grails-core/src/main/groovy/org/grails/config/CompositeConfig.java @@ -18,13 +18,14 @@ */ package org.grails.config; +import java.util.AbstractMap; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collection; import java.util.Deque; -import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Properties; @@ -37,7 +38,9 @@ import grails.util.GrailsStringUtils; import org.grails.core.exceptions.GrailsConfigurationException; /** - * A {@link Config} composed of other Configs + * A {@link Config} composed of other Configs. A lookup answers from the first config, in order of precedence, + * that has the key, and the {@link Map} views, {@link #flatten()} and {@link #toProperties()} resolve a key + * shared by several configs the same way, so each key appears once with its highest-precedence value. * * @author Graeme Rocher * @since 3.0 @@ -69,8 +72,9 @@ public class CompositeConfig implements Config { @Deprecated public Map<String, Object> flatten() { Map<String, Object> flattened = new LinkedHashMap<>(); - for (Config c : configs) { - flattened.putAll(c.flatten()); + Iterator<Config> lowestPrecedenceFirst = configs.descendingIterator(); + while (lowestPrecedenceFirst.hasNext()) { + flattened.putAll(lowestPrecedenceFirst.next().flatten()); } return flattened; } @@ -78,8 +82,9 @@ public class CompositeConfig implements Config { @Override public Properties toProperties() { Properties properties = new Properties(); - for (Config c : configs) { - properties.putAll(c.toProperties()); + Iterator<Config> lowestPrecedenceFirst = configs.descendingIterator(); + while (lowestPrecedenceFirst.hasNext()) { + properties.putAll(lowestPrecedenceFirst.next().toProperties()); } return properties; } @@ -100,6 +105,11 @@ public class CompositeConfig implements Config { @Override public Object getAt(Object key) { + for (Config c : configs) { + if (c.containsKey(key)) { + return c.getAt(key); + } + } for (Config c : configs) { Object v = c.getAt(key); if (v != null) return v; @@ -123,11 +133,7 @@ public class CompositeConfig implements Config { @Override public int size() { - int size = 0; - for (Config config : configs) { - size += config.size(); - } - return size; + return keySet().size(); } @Override @@ -150,14 +156,21 @@ public class CompositeConfig implements Config { @Override public boolean containsValue(Object value) { - for (Config config : configs) { - if (config.containsValue(value)) return true; - } - return false; + return values().contains(value); } + /** + * The value of the highest precedence config that has the key. A config answers a key it does not have with a + * navigator for the keys below it, so a config is only asked when it has the key, and the first answer of any + * config is returned only when none has it. + */ @Override public Object get(Object key) { + for (Config config : configs) { + if (config.containsKey(key)) { + return config.get(key); + } + } for (Config config : configs) { Object v = config.get(key); if (v != null) return v; @@ -192,27 +205,27 @@ public class CompositeConfig implements Config { @Override public Set<String> keySet() { - Set<String> entries = new HashSet<>(); + Set<String> keys = new LinkedHashSet<>(); for (Config config : configs) { - entries.addAll(config.keySet()); + keys.addAll(config.keySet()); } - return entries; + return keys; } @Override public Collection<Object> values() { Collection<Object> values = new ArrayList<>(); - for (Config config : configs) { - values.addAll(config.values()); + for (String key : keySet()) { + values.add(get(key)); } return values; } @Override public Set<Map.Entry<String, Object>> entrySet() { - Set<Map.Entry<String, Object>> entries = new HashSet<>(); - for (Config config : configs) { - entries.addAll(config.entrySet()); + Set<Map.Entry<String, Object>> entries = new LinkedHashSet<>(); + for (String key : keySet()) { + entries.add(new AbstractMap.SimpleImmutableEntry<>(key, get(key))); } return entries; } diff --git a/grails-core/src/test/groovy/org/grails/config/CompositeConfigSpec.groovy b/grails-core/src/test/groovy/org/grails/config/CompositeConfigSpec.groovy index 0be4e139f6..79dd612aec 100644 --- a/grails-core/src/test/groovy/org/grails/config/CompositeConfigSpec.groovy +++ b/grails-core/src/test/groovy/org/grails/config/CompositeConfigSpec.groovy @@ -56,19 +56,18 @@ class CompositeConfigSpec extends Specification { config.navigate('service', 'missing') == null } - void 'flatten and properties combine the keys of every config'() { + void 'flatten and properties resolve a shared key to the highest precedence config'() { given: def config = new CompositeConfig() - config.addFirst(new PropertySourcesConfig([shared: 'first', firstOnly: 'one'])) + config.addLast(new PropertySourcesConfig([shared: 'middle', middleOnly: 'mid'])) config.addLast(new PropertySourcesConfig([shared: 'last', lastOnly: 'two'])) + config.addFirst(new PropertySourcesConfig([shared: 'first', firstOnly: 'one'])) expect: - config.flatten().keySet() == ['shared', 'firstOnly', 'lastOnly'] as Set - config.flatten().subMap(['firstOnly', 'lastOnly']) == [firstOnly: 'one', lastOnly: 'two'] - config.toProperties().keySet() == ['shared', 'firstOnly', 'lastOnly'] as Set - config.toProperties().getProperty('firstOnly') == 'one' - config.toProperties().getProperty('lastOnly') == 'two' - config.getProperty('shared') == 'first' + config.flatten() == [shared: 'first', firstOnly: 'one', middleOnly: 'mid', lastOnly: 'two'] + config.toProperties() == [shared: 'first', firstOnly: 'one', middleOnly: 'mid', lastOnly: 'two'] as Properties + config.flatten().shared == config.getProperty('shared') + config.toProperties().getProperty('shared') == config.getProperty('shared') } void 'flatten and properties resolve disjoint configs completely'() { @@ -83,7 +82,7 @@ class CompositeConfigSpec extends Specification { config.size() == 2 } - void 'map views include the keys of every config'() { + void 'map views carry every key once with its highest precedence value'() { given: def first = new PropertySourcesConfig([shared: 'first', firstOnly: 'one']) def last = new PropertySourcesConfig([shared: 'last', lastOnly: 'two']) @@ -93,6 +92,10 @@ class CompositeConfigSpec extends Specification { expect: !config.isEmpty() + config.size() == 3 + config.get('lastOnly') == 'two' + config.getAt('lastOnly') == 'two' + config.get('shared') == 'first' config.keySet() == ['shared', 'firstOnly', 'lastOnly'] as Set config.containsKey('firstOnly') config.containsKey('lastOnly') @@ -102,13 +105,30 @@ class CompositeConfigSpec extends Specification { config.containsValue('first') config.containsValue('two') !config.containsValue('missing') - config.values().containsAll(['first', 'one', 'two']) + !config.containsValue('last') + config.values().size() == 3 + config.values().toSet() == ['first', 'one', 'two'] as Set + config.entrySet().size() == 3 config.entrySet()*.key.toSet() == config.keySet() + config.entrySet().find { it.key == 'shared' }.value == 'first' config.entrySet().find { it.key == 'firstOnly' }.value == 'one' config.entrySet().find { it.key == 'lastOnly' }.value == 'two' config.iterator().toList().toSet() == config.entrySet() } + void 'map views keep the precedence order of the configs that contribute keys'() { + given: + def config = new CompositeConfig() + config.addLast(new PropertySourcesConfig([lastOnly: 'two', shared: 'last'])) + config.addFirst(new PropertySourcesConfig([shared: 'first', firstOnly: 'one'])) + + expect: 'the keys of the highest precedence config come first, then the keys only lower configs have' + config.keySet().toList().take(2).toSet() == ['shared', 'firstOnly'] as Set + config.keySet().toList().last() == 'lastOnly' + config.entrySet()*.key == config.keySet().toList() + config.values().toList() == config.keySet().collect { config.get(it) } + } + void 'empty composites and empty delegates have empty views'() { given: def config = new CompositeConfig()
