Skip to content

Commit d1c7699

Browse files
committed
Merge branch '2.1.x' into 2.2.x
Closes gh-20129
2 parents 8f06d81 + 3917968 commit d1c7699

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
class CloudFoundryVcapEnvironmentPostProcessorTests {
3537

@@ -116,6 +118,26 @@ void testServicePropertiesWithoutNA() {
116118
assertThat(getProperty("vcap.services.mysql.credentials.port")).isEqualTo("3306");
117119
}
118120

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

0 commit comments

Comments
 (0)