diff --git a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySource.java b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySource.java index 983c931b..b1737f79 100644 --- a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySource.java +++ b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySource.java @@ -63,6 +63,9 @@ public class ConsulPropertySource extends EnumerablePropertySource if (!this.context.endsWith("/")) { this.context = this.context + "/"; } + if (this.context.startsWith("/")) { + this.context = this.context.substring(1); + } Response> response = this.source.getKVValues(this.context, this.configProperties.getAclToken(), QueryParams.DEFAULT); diff --git a/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertySourceTests.java b/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertySourceTests.java index eeac72ba..f9a021d1 100644 --- a/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertySourceTests.java +++ b/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertySourceTests.java @@ -43,7 +43,7 @@ public class ConsulPropertySourceTests { @Before public void setup() { ConsulTestcontainers.start(); - this.prefix = "consulPropertySourceTests" + new Random().nextInt(Integer.MAX_VALUE); + this.prefix = "/consulPropertySourceTests" + new Random().nextInt(Integer.MAX_VALUE); this.client = ConsulTestcontainers.client(); } @@ -117,4 +117,12 @@ public class ConsulPropertySourceTests { return source; } + @Test + public void testExtraSlashInContext() { + ConsulPropertySource source = new ConsulPropertySource("/my/very/custom/context", this.client, + new ConsulConfigProperties()); + source.init(); + assertThat(source.getContext()).as("context was wrong").isEqualTo("my/very/custom/context/"); + } + }