Skip to content

Commit 544dca7

Browse files
committed
Polish contribution
Along with surrounding map keys with dot from VCAP_SERVICES with `[ ]`, this commit also does that for non-alphanumeric and `-` characters so that they are not stripped off later. See gh-18915
1 parent 6828a15 commit 544dca7

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

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

Lines changed: 11 additions & 1 deletion
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,10 +231,19 @@ private String getPropertyName(String path, String key) {
230231
if (key.startsWith("[")) {
231232
return path + key;
232233
}
233-
if (key.contains(".")) {
234+
if (shouldWrap(key)) {
234235
return path + "[" + key + "]";
235236
}
236237
return path + "." + key;
237238
}
238239

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+
239249
}

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

Lines changed: 13 additions & 1 deletion
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

@@ -118,7 +120,7 @@ public void testServicePropertiesWithoutNA() {
118120
}
119121

120122
@Test
121-
void testServicePropertiesContainingKeysWithDot() {
123+
public void testServicePropertiesContainingKeysWithDot() {
122124
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
123125
"VCAP_SERVICES={\"user-provided\":[{\"name\":\"test\",\"label\":\"test-label\","
124126
+ "\"credentials\":{\"key.with.dots\":\"some-value\"}}]}");
@@ -127,6 +129,16 @@ void testServicePropertiesContainingKeysWithDot() {
127129
assertThat(getProperty("vcap.services.test.credentials[key.with.dots]")).isEqualTo("some-value");
128130
}
129131

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+
130142
private String getProperty(String key) {
131143
return this.context.getEnvironment().getProperty(key);
132144
}

0 commit comments

Comments
 (0)