Skip to content

Commit

Permalink
Merge pull request #18915 from schulzh
Browse files Browse the repository at this point in the history
* pr/18915:
  Polish contribution
  Handle JSON keys containing a dot from CF environment as a single path segment

Closes gh-18915
  • Loading branch information
mbhave committed Feb 11, 2020
2 parents b0aba9e + 544dca7 commit 3917968
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Expand Up @@ -87,6 +87,7 @@
*
* @author Dave Syer
* @author Andy Wilkinson
* @author Madhura Bhave
* @since 1.3.0
*/
public class CloudFoundryVcapEnvironmentPostProcessor
Expand Down Expand Up @@ -230,7 +231,19 @@ private String getPropertyName(String path, String key) {
if (key.startsWith("[")) {
return path + key;
}
if (shouldWrap(key)) {
return path + "[" + key + "]";
}
return path + "." + key;
}

private boolean shouldWrap(String key) {
for (char ch : key.toCharArray()) {
if (!Character.isLowerCase(ch) && !Character.isDigit(ch) && ch != '-') {
return true;
}
}
return false;
}

}
Expand Up @@ -30,6 +30,8 @@
*
* @author Dave Syer
* @author Andy Wilkinson
* @author Hans Schulz
* @author Madhura Bhave
*/
public class CloudFoundryVcapEnvironmentPostProcessorTests {

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

@Test
public void testServicePropertiesContainingKeysWithDot() {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"VCAP_SERVICES={\"user-provided\":[{\"name\":\"test\",\"label\":\"test-label\","
+ "\"credentials\":{\"key.with.dots\":\"some-value\"}}]}");
this.initializer.postProcessEnvironment(this.context.getEnvironment(), null);
assertThat(getProperty("vcap.services.test.name")).isEqualTo("test");
assertThat(getProperty("vcap.services.test.credentials[key.with.dots]")).isEqualTo("some-value");
}

@Test
public void testServicePropertiesContainingKeysWithUpperCaseAndNonAlphaNumericCharacters() {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"VCAP_SERVICES={\"user-provided\":[{\"name\":\"test\",\"label\":\"test-label\","
+ "\"credentials\":{\"My-Key\":\"some-value\", \"foo@\":\"bar\"}}]}");
this.initializer.postProcessEnvironment(this.context.getEnvironment(), null);
assertThat(getProperty("vcap.services.test.credentials[My-Key]")).isEqualTo("some-value");
assertThat(getProperty("vcap.services.test.credentials[foo@]")).isEqualTo("bar");
}

private String getProperty(String key) {
return this.context.getEnvironment().getProperty(key);
}
Expand Down

0 comments on commit 3917968

Please sign in to comment.