Browse Source

Preserve property ordering in SpringIterableConfigurationPropertySource

Fixes gh-21470
pull/21492/head
Andy Wilkinson 6 years ago
parent
commit
beb7cb4b81
  1. 4
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringIterableConfigurationPropertySource.java
  2. 14
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringIterableConfigurationPropertySourceTests.java

4
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringIterableConfigurationPropertySource.java

@ -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.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;
@ -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) {
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,

14
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringIterableConfigurationPropertySourceTests.java

@ -224,6 +224,20 @@ class SpringIterableConfigurationPropertySourceTests { @@ -224,6 +224,20 @@ class SpringIterableConfigurationPropertySourceTests {
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}.
*

Loading…
Cancel
Save