From 147755f396cad20c393e6b7a491416b2fac8fbb3 Mon Sep 17 00:00:00 2001 From: Ben Hale Date: Tue, 24 Mar 2015 15:55:25 +0000 Subject: [PATCH] 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 --- .../boot/cloudfoundry/VcapApplicationListener.java | 5 ++++- .../cloudfoundry/VcapApplicationListenerTests.java | 10 +++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/cloudfoundry/VcapApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/cloudfoundry/VcapApplicationListener.java index 72cff54bfed..3635f71d884 100644 --- a/spring-boot/src/main/java/org/springframework/boot/cloudfoundry/VcapApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/cloudfoundry/VcapApplicationListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2014 the original author or authors. + * Copyright 2010-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -213,6 +213,9 @@ public class VcapApplicationListener implements if (value instanceof String) { properties.put(key, value); } + else if (value instanceof Number) { + properties.put(key, value.toString()); + } else if (value instanceof Map) { // Need a compound key @SuppressWarnings("unchecked") diff --git a/spring-boot/src/test/java/org/springframework/boot/cloudfoundry/VcapApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/cloudfoundry/VcapApplicationListenerTests.java index 6cce6e7685a..d0a0675d4fb 100644 --- a/spring-boot/src/test/java/org/springframework/boot/cloudfoundry/VcapApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/cloudfoundry/VcapApplicationListenerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -75,6 +75,10 @@ public class VcapApplicationListenerTests { this.initializer.onApplicationEvent(this.event); assertEquals("mysql", this.context.getEnvironment().getProperty("vcap.services.mysql.name")); + assertEquals( + "3306", + this.context.getEnvironment().getProperty( + "vcap.services.mysql.credentials.port")); } @Test @@ -86,5 +90,9 @@ public class VcapApplicationListenerTests { this.initializer.onApplicationEvent(this.event); assertEquals("mysql", this.context.getEnvironment().getProperty("vcap.services.mysql.name")); + assertEquals( + "3306", + this.context.getEnvironment().getProperty( + "vcap.services.mysql.credentials.port")); } }