Skip to content

Commit 3917968

Browse files
committed
Merge pull request #18915 from schulzh
* pr/18915: Polish contribution Handle JSON keys containing a dot from CF environment as a single path segment Closes gh-18915
2 parents b0aba9e + 544dca7 commit 3917968

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudFoundryVcapEnvironmentPostProcessor.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
*
8888
* @author Dave Syer
8989
* @author Andy Wilkinson
90+
* @author Madhura Bhave
9091
* @since 1.3.0
9192
*/
9293
public class CloudFoundryVcapEnvironmentPostProcessor
@@ -230,7 +231,19 @@ private String getPropertyName(String path, String key) {
230231
if (key.startsWith("[")) {
231232
return path + key;
232233
}
234+
if (shouldWrap(key)) {
235+
return path + "[" + key + "]";
236+
}
233237
return path + "." + key;
234238
}
235239

240+
private boolean shouldWrap(String key) {
241+
for (char ch : key.toCharArray()) {
242+
if (!Character.isLowerCase(ch) && !Character.isDigit(ch) && ch != '-') {
243+
return true;
244+
}
245+
}
246+
return false;
247+
}
248+
236249
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/cloudfoundry/CloudFoundryVcapEnvironmentPostProcessorTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
*
3131
* @author Dave Syer
3232
* @author Andy Wilkinson
33+
* @author Hans Schulz
34+
* @author Madhura Bhave
3335
*/
3436
public class CloudFoundryVcapEnvironmentPostProcessorTests {
3537

@@ -117,6 +119,26 @@ public void testServicePropertiesWithoutNA() {
117119
assertThat(getProperty("vcap.services.mysql.credentials.port")).isEqualTo("3306");
118120
}
119121

122+
@Test
123+
public void testServicePropertiesContainingKeysWithDot() {
124+
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
125+
"VCAP_SERVICES={\"user-provided\":[{\"name\":\"test\",\"label\":\"test-label\","
126+
+ "\"credentials\":{\"key.with.dots\":\"some-value\"}}]}");
127+
this.initializer.postProcessEnvironment(this.context.getEnvironment(), null);
128+
assertThat(getProperty("vcap.services.test.name")).isEqualTo("test");
129+
assertThat(getProperty("vcap.services.test.credentials[key.with.dots]")).isEqualTo("some-value");
130+
}
131+
132+
@Test
133+
public void testServicePropertiesContainingKeysWithUpperCaseAndNonAlphaNumericCharacters() {
134+
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
135+
"VCAP_SERVICES={\"user-provided\":[{\"name\":\"test\",\"label\":\"test-label\","
136+
+ "\"credentials\":{\"My-Key\":\"some-value\", \"foo@\":\"bar\"}}]}");
137+
this.initializer.postProcessEnvironment(this.context.getEnvironment(), null);
138+
assertThat(getProperty("vcap.services.test.credentials[My-Key]")).isEqualTo("some-value");
139+
assertThat(getProperty("vcap.services.test.credentials[foo@]")).isEqualTo("bar");
140+
}
141+
120142
private String getProperty(String key) {
121143
return this.context.getEnvironment().getProperty(key);
122144
}

0 commit comments

Comments
 (0)