Commit beb7cb4b authored by Andy Wilkinson's avatar Andy Wilkinson

Preserve property ordering in SpringIterableConfigurationPropertySource

Fixes gh-21470
parent 4f31c3bf
...@@ -19,9 +19,9 @@ package org.springframework.boot.context.properties.source; ...@@ -19,9 +19,9 @@ package org.springframework.boot.context.properties.source;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.ConcurrentModificationException; import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
...@@ -259,7 +259,7 @@ class SpringIterableConfigurationPropertySource extends SpringConfigurationPrope ...@@ -259,7 +259,7 @@ class SpringIterableConfigurationPropertySource extends SpringConfigurationPrope
} }
private <K, V> Map<K, V> cloneOrCreate(Map<K, V> source, int size) { private <K, V> Map<K, V> cloneOrCreate(Map<K, V> source, int size) {
return (source != null) ? new HashMap<>(source) : new HashMap<>(size); return (source != null) ? new LinkedHashMap<>(source) : new LinkedHashMap<>(size);
} }
private void addParents(Map<ConfigurationPropertyName, Set<ConfigurationPropertyName>> descendants, private void addParents(Map<ConfigurationPropertyName, Set<ConfigurationPropertyName>> descendants,
......
...@@ -224,6 +224,20 @@ class SpringIterableConfigurationPropertySourceTests { ...@@ -224,6 +224,20 @@ class SpringIterableConfigurationPropertySourceTests {
assertThat(adapter.stream()).hasSize(2); assertThat(adapter.stream()).hasSize(2);
} }
@Test
void orderOfUnderlyingSourceIsPreserved() {
Map<String, Object> map = new LinkedHashMap<>();
map.put("test.map.alpha", "value1");
map.put("test.map.bravo", "value2");
map.put("test.map.charlie", "value3");
map.put("test.map.delta", "value4");
EnumerablePropertySource<?> source = new OriginTrackedMapPropertySource("test", map, true);
SpringIterableConfigurationPropertySource propertySource = new SpringIterableConfigurationPropertySource(source,
DefaultPropertyMapper.INSTANCE);
assertThat(propertySource.stream().map(ConfigurationPropertyName::toString)).containsExactly("test.map.alpha",
"test.map.bravo", "test.map.charlie", "test.map.delta");
}
/** /**
* Test {@link PropertySource} that's also an {@link OriginLookup}. * Test {@link PropertySource} that's also an {@link OriginLookup}.
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment