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
This commit is contained in:
Madhura Bhave
2020-02-11 14:47:25 -08:00
parent 6828a15d31
commit 544dca7f1b
2 changed files with 24 additions and 2 deletions

View File

@@ -87,6 +87,7 @@ import org.springframework.util.StringUtils;
*
* @author Dave Syer
* @author Andy Wilkinson
* @author Madhura Bhave
* @since 1.3.0
*/
public class CloudFoundryVcapEnvironmentPostProcessor
@@ -230,10 +231,19 @@ public class CloudFoundryVcapEnvironmentPostProcessor
if (key.startsWith("[")) {
return path + key;
}
if (key.contains(".")) {
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;
}
}

View File

@@ -30,6 +30,8 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Dave Syer
* @author Andy Wilkinson
* @author Hans Schulz
* @author Madhura Bhave
*/
public class CloudFoundryVcapEnvironmentPostProcessorTests {
@@ -118,7 +120,7 @@ public class CloudFoundryVcapEnvironmentPostProcessorTests {
}
@Test
void testServicePropertiesContainingKeysWithDot() {
public void testServicePropertiesContainingKeysWithDot() {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"VCAP_SERVICES={\"user-provided\":[{\"name\":\"test\",\"label\":\"test-label\","
+ "\"credentials\":{\"key.with.dots\":\"some-value\"}}]}");
@@ -127,6 +129,16 @@ public class CloudFoundryVcapEnvironmentPostProcessorTests {
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);
}