From b353227d07d8deaabe5e701f3f1e15fa23b10a6e Mon Sep 17 00:00:00 2001 From: Aled Sage Date: Mon, 13 Nov 2017 11:20:01 +0000 Subject: [PATCH] BROOKLYN-461: coerce empty string to URI as null --- .../util/javalang/coerce/CommonAdaptorTypeCoercions.java | 2 +- .../brooklyn/util/javalang/coerce/TypeCoercionsTest.java | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/javalang/coerce/CommonAdaptorTypeCoercions.java b/utils/common/src/main/java/org/apache/brooklyn/util/javalang/coerce/CommonAdaptorTypeCoercions.java index 8d6e92494a..4072bc1926 100644 --- a/utils/common/src/main/java/org/apache/brooklyn/util/javalang/coerce/CommonAdaptorTypeCoercions.java +++ b/utils/common/src/main/java/org/apache/brooklyn/util/javalang/coerce/CommonAdaptorTypeCoercions.java @@ -148,7 +148,7 @@ public String apply(URL input) { registerAdapter(String.class, URI.class, new Function() { @Override public URI apply(String input) { - return URI.create(input); + return Strings.isNonBlank(input) ? URI.create(input) : null; } }); registerAdapter(URI.class, String.class, new Function() { diff --git a/utils/common/src/test/java/org/apache/brooklyn/util/javalang/coerce/TypeCoercionsTest.java b/utils/common/src/test/java/org/apache/brooklyn/util/javalang/coerce/TypeCoercionsTest.java index 74312ff9a9..c5e989b725 100644 --- a/utils/common/src/test/java/org/apache/brooklyn/util/javalang/coerce/TypeCoercionsTest.java +++ b/utils/common/src/test/java/org/apache/brooklyn/util/javalang/coerce/TypeCoercionsTest.java @@ -398,6 +398,15 @@ public void testURItoStringCoercion() { Assert.assertEquals(s, "http://localhost:1234/"); } + @Test + public void testStringToUriCoercion() { + Assert.assertEquals(coerce("http://localhost:1234/", URI.class), URI.create("http://localhost:1234/")); + + // Empty string is coerced to null; in contrast, `URI.create("")` gives a URI + // with null scheme, host, etc (which would be very surprising for Brooklyn users!). + Assert.assertEquals(coerce("", URI.class), null); + } + @Test public void testURLtoStringCoercion() throws MalformedURLException { String s = coerce(new URL("http://localhost:1234/"), String.class);