diff --git a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java index 11fdd8f4..5adcf222 100644 --- a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java +++ b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java @@ -176,6 +176,9 @@ public class ConfigServicePropertySourceLocator implements PropertySourceLocator Object[] args = new String[] { name, profile }; if (StringUtils.hasText(label)) { + if (label.contains("/")) { + label = label.replace("/", "(_)"); + } args = new String[] { name, profile, label }; path = path + "/{label}"; } diff --git a/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java index 7087d32c..71ca4182 100644 --- a/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java +++ b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java @@ -67,6 +67,17 @@ public class ConfigServicePropertySourceLocatorTests { assertNotNull(this.locator.locate(this.environment)); } + @Test + public void sunnyDayWithLabelThatContainsASlash() { + Environment body = new Environment("app", "master"); + mockRequestResponseWithLabel( + new ResponseEntity<>(body, HttpStatus.OK), "release(_)v1.0.0"); + this.locator.setRestTemplate(this.restTemplate); + EnvironmentTestUtils.addEnvironment(this.environment, + "spring.cloud.config.label:release/v1.0.0"); + assertNotNull(this.locator.locate(this.environment)); + } + @Test public void sunnyDayWithNoSuchLabel() { mockRequestResponseWithLabel(new ResponseEntity((Void) null, @@ -235,4 +246,13 @@ public class ConfigServicePropertySourceLocatorTests { Mockito.any(Class.class), Matchers.anyString(), Matchers.anyString())).thenReturn(response); } + + @SuppressWarnings("unchecked") + private void mockRequestResponseWithoutLabelWithExpectedName(ResponseEntity response, String expectedName) { + Mockito.when( + this.restTemplate.exchange(Mockito.any(String.class), + Mockito.any(HttpMethod.class), Mockito.any(HttpEntity.class), + Mockito.any(Class.class), Matchers.eq(expectedName), + Matchers.anyString())).thenReturn(response); + } }