From 8c2560b113e55c250d4a9e118cb2b2a9b5fa15d6 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 28 Jan 2015 14:11:20 +0000 Subject: [PATCH] Fix YAML endpoints when there are multiple property sources Also affects properties endpoints. The order of property sources should be reversed when creating the properties to mimic accurately the behaviuour of Spring's Environment. Fixes gh-71 --- .../cloud/config/server/EnvironmentController.java | 4 +++- .../config/server/EnvironmentControllerTests.java | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/EnvironmentController.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/EnvironmentController.java index 31172579..223ca391 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/EnvironmentController.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/EnvironmentController.java @@ -180,7 +180,9 @@ public class EnvironmentController { private Properties convertToProperties(Environment profiles) { Properties map = new Properties(); - for (PropertySource source : profiles.getPropertySources()) { + List sources = new ArrayList(profiles.getPropertySources()); + Collections.reverse(sources); + for (PropertySource source : sources) { @SuppressWarnings("unchecked") Map value = (Map) source.getSource(); map.putAll(value); diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/EnvironmentControllerTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/EnvironmentControllerTests.java index 8743875f..a8d80268 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/EnvironmentControllerTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/EnvironmentControllerTests.java @@ -60,6 +60,17 @@ public class EnvironmentControllerTests { assertEquals("a:\n b:\n c: d\n", yaml); } + @Test + public void propertyOverrideInYaml() throws Exception { + Map map = new HashMap(); + map.put("a.b.c", "d"); + environment.add(new PropertySource("one", map)); + environment.addFirst(new PropertySource("two", Collections.singletonMap("a.b.c", "e"))); + Mockito.when(repository.findOne("foo", "bar", "master")).thenReturn(environment); + String yaml = controller.yaml("foo", "bar").getBody(); + assertEquals("a:\n b:\n c: e\n", yaml); + } + @Test public void arrayInYaml() throws Exception { Map map = new LinkedHashMap();