From 0075653f807e16d92d7c0675c39d7a7301f2e98d Mon Sep 17 00:00:00 2001 From: Chen Li <39090842+LeonhardtC@users.noreply.github.com> Date: Fri, 5 May 2023 04:54:11 +0800 Subject: [PATCH] fix issue #2245, related to #385 (#2247) --- .../environment/EnvironmentController.java | 38 +++++++------------ 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/EnvironmentController.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/EnvironmentController.java index b01b1b52..d33f6191 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/EnvironmentController.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/EnvironmentController.java @@ -61,6 +61,7 @@ import static org.springframework.cloud.config.server.support.EnvironmentPropert * @author Ivan Corrales Solera * @author Daniel Frey * @author Ian Bondoc + * @author Chen Li * */ @RestController @@ -291,45 +292,32 @@ public class EnvironmentController { } private Map convertToProperties(Environment profiles) { - - // Map of unique keys containing full map of properties for each unique - // key - Map> map = new LinkedHashMap<>(); List sources = new ArrayList<>(profiles.getPropertySources()); Collections.reverse(sources); Map combinedMap = new LinkedHashMap<>(); + Map> arrayMap = new LinkedHashMap<>(); for (PropertySource source : sources) { - @SuppressWarnings("unchecked") Map value = (Map) source.getSource(); - for (String key : value.keySet()) { - - if (!key.contains("[")) { - - // Not an array, add unique key to the map - combinedMap.put(key, value.get(key)); + Map> currentArrayMap = new LinkedHashMap<>(); + for (Entry entry : value.entrySet()) { + if (!entry.getKey().contains("[")) { + combinedMap.put(entry.getKey(), entry.getValue()); } else { - - // An existing array might have already been added to the property map - // of an unequal size to the current array. Replace the array key in - // the current map. - key = key.substring(0, key.indexOf("[")); - Map filtered = new LinkedHashMap<>(); - for (String index : value.keySet()) { - if (index.startsWith(key + "[")) { - filtered.put(index, value.get(index)); - } - } - map.put(key, filtered); + String prefixKey = entry.getKey().substring(0, + entry.getKey().indexOf("[")); + currentArrayMap.computeIfAbsent(prefixKey, k -> new LinkedHashMap<>()) + .put(entry.getKey(), entry.getValue()); } } - + // Override array properties by prefix key + arrayMap.putAll(currentArrayMap); } // Combine all unique keys for array values into the combined map - for (Entry> entry : map.entrySet()) { + for (Entry> entry : arrayMap.entrySet()) { combinedMap.putAll(entry.getValue()); }