Skip to content

Commit

Permalink
Preserve property ordering in SpringIterableConfigurationPropertySource
Browse files Browse the repository at this point in the history
Fixes gh-21470
  • Loading branch information
wilkinsona committed May 18, 2020
1 parent 4f31c3b commit beb7cb4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
Expand Down Expand Up @@ -259,7 +259,7 @@ private void updateMappings(String[] propertyNames) {
}

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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,20 @@ void readOnlyOriginTrackedMapPropertySourceKeyAdditionDoesNotInvalidateCache() {
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}.
*
Expand Down

0 comments on commit beb7cb4

Please sign in to comment.