diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/support/EnvironmentPropertySource.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/support/EnvironmentPropertySource.java index 2a6a06c2..00a47c28 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/support/EnvironmentPropertySource.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/support/EnvironmentPropertySource.java @@ -13,13 +13,17 @@ public class EnvironmentPropertySource extends PropertySource { public static StandardEnvironment prepareEnvironment(Environment environment) { StandardEnvironment standardEnvironment = new StandardEnvironment(); - standardEnvironment.getPropertySources().addAfter( - StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, - new EnvironmentPropertySource(environment)); + standardEnvironment.getPropertySources() + .remove(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME); + standardEnvironment.getPropertySources() + .remove(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME); + standardEnvironment.getPropertySources() + .addFirst(new EnvironmentPropertySource(environment)); return standardEnvironment; } - public static String resolvePlaceholders(StandardEnvironment preparedEnvironment, String text) { + public static String resolvePlaceholders(StandardEnvironment preparedEnvironment, + String text) { // Mask out escaped placeholders text = text.replace("\\${", "$_{"); return preparedEnvironment.resolvePlaceholders(text).replace("$_{", "${"); diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerTests.java index 19322bd4..3c5ee24a 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerTests.java @@ -28,6 +28,7 @@ import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -63,6 +64,11 @@ public class EnvironmentControllerTests { this.controller = new EnvironmentController(this.repository); } + @After + public void clean() { + System.clearProperty("foo"); + } + @Test public void vanillaYaml() throws Exception { Map map = new HashMap(); @@ -107,6 +113,20 @@ public class EnvironmentControllerTests { assertEquals("a:\n b:\n c: bar\nfoo: bar\n", yaml); } + @Test + public void placeholdersNotResolvedInYaml() throws Exception { + whenPlaceholders(); + String yaml = this.controller.yaml("foo", "bar", false).getBody(); + assertEquals("a:\n b:\n c: ${foo}\nfoo: bar\n", yaml); + } + + @Test + public void placeholdersNotResolvedInYamlFromSystemProperties() throws Exception { + whenPlaceholdersSystemProps(); + String yaml = this.controller.yaml("foo", "bar", true).getBody(); + assertEquals("a:\n b:\n c: ${foo}\n", yaml); + } + @Test public void arrayInYaml() throws Exception { Map map = new LinkedHashMap(); @@ -243,6 +263,20 @@ public class EnvironmentControllerTests { assertEquals("a.b.c: bar\nfoo: bar", text); } + @Test + public void placeholdersNotResolvedInProperties() throws Exception { + whenPlaceholders(); + String text = this.controller.properties("foo", "bar", false).getBody(); + assertEquals("a.b.c: ${foo}\nfoo: bar", text); + } + + @Test + public void placeholdersNotResolvedInPropertiesFromSystemProperties() throws Exception { + whenPlaceholdersSystemProps(); + String text = this.controller.properties("foo", "bar", true).getBody(); + assertEquals("a.b.c: ${foo}", text); + } + @Test public void placeholdersResolvedInJson() throws Exception { whenPlaceholders(); @@ -250,6 +284,20 @@ public class EnvironmentControllerTests { assertEquals("{\"a\":{\"b\":{\"c\":\"bar\"}},\"foo\":\"bar\"}", json); } + @Test + public void placeholdersNotResolvedInJson() throws Exception { + whenPlaceholders(); + String json = this.controller.jsonProperties("foo", "bar", false).getBody(); + assertEquals("{\"a\":{\"b\":{\"c\":\"${foo}\"}},\"foo\":\"bar\"}", json); + } + + @Test + public void placeholdersNotResolvedInJsonFromSystemProperties() throws Exception { + whenPlaceholdersSystemProps(); + String json = this.controller.jsonProperties("foo", "bar", true).getBody(); + assertEquals("{\"a\":{\"b\":{\"c\":\"${foo}\"}}}", json); + } + private void whenPlaceholders() { Map map = new LinkedHashMap(); map.put("foo", "bar"); @@ -258,6 +306,12 @@ public class EnvironmentControllerTests { Mockito.when(this.repository.findOne("foo", "bar", null)).thenReturn(this.environment); } + private void whenPlaceholdersSystemProps() { + System.setProperty("foo", "bar"); + this.environment.addFirst(new PropertySource("two", Collections.singletonMap("a.b.c", "${foo}"))); + Mockito.when(this.repository.findOne("foo", "bar", null)).thenReturn(this.environment); + } + @Test public void mappingForEnvironment() throws Exception { Mockito.when(this.repository.findOne("foo", "bar", null)).thenReturn(this.environment);