Skip to content

Commit 147755f

Browse files
nebhalephilwebb
authored andcommitted
Support VcapApplicationListener Number Credentials
Update VcapApplicationListener to support Number based credentials. Previously, any service credential value that wasn't a String would be discarded. This was particularly a problem for services that exposed a port as a JSON Number. This change takes numbers in the credential payload into account, converting them to Strings so that they will pass through the properties system properly. There's no real downside to this as Spring will coerce them back into Numbers if needed by an application. Fixes gh-2707
1 parent 1fba81e commit 147755f

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

spring-boot/src/main/java/org/springframework/boot/cloudfoundry/VcapApplicationListener.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2014 the original author or authors.
2+
* Copyright 2010-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -213,6 +213,9 @@ private void flatten(Properties properties, Map<String, Object> input, String pa
213213
if (value instanceof String) {
214214
properties.put(key, value);
215215
}
216+
else if (value instanceof Number) {
217+
properties.put(key, value.toString());
218+
}
216219
else if (value instanceof Map) {
217220
// Need a compound key
218221
@SuppressWarnings("unchecked")

spring-boot/src/test/java/org/springframework/boot/cloudfoundry/VcapApplicationListenerTests.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -75,6 +75,10 @@ public void testServiceProperties() {
7575
this.initializer.onApplicationEvent(this.event);
7676
assertEquals("mysql",
7777
this.context.getEnvironment().getProperty("vcap.services.mysql.name"));
78+
assertEquals(
79+
"3306",
80+
this.context.getEnvironment().getProperty(
81+
"vcap.services.mysql.credentials.port"));
7882
}
7983

8084
@Test
@@ -86,5 +90,9 @@ public void testServicePropertiesWithoutNA() {
8690
this.initializer.onApplicationEvent(this.event);
8791
assertEquals("mysql",
8892
this.context.getEnvironment().getProperty("vcap.services.mysql.name"));
93+
assertEquals(
94+
"3306",
95+
this.context.getEnvironment().getProperty(
96+
"vcap.services.mysql.credentials.port"));
8997
}
9098
}

0 commit comments

Comments
 (0)