diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringIterableConfigurationPropertySource.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringIterableConfigurationPropertySource.java index a022588ba2..c7cdd36f4c 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringIterableConfigurationPropertySource.java +++ b/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; 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 } private Map cloneOrCreate(Map 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> descendants, diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringIterableConfigurationPropertySourceTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringIterableConfigurationPropertySourceTests.java index f9b78e5aae..4493d596bb 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringIterableConfigurationPropertySourceTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringIterableConfigurationPropertySourceTests.java @@ -224,6 +224,20 @@ class SpringIterableConfigurationPropertySourceTests { assertThat(adapter.stream()).hasSize(2); } + @Test + void orderOfUnderlyingSourceIsPreserved() { + Map 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}. *