This is an automated email from the ASF dual-hosted git repository.

iuliana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git


The following commit(s) were added to refs/heads/master by this push:
     new 8eb5feee03 fix gap in recent deserialization improvements to 
deserialize empty/default items
     new 37de17af3a Merge pull request #1317 from 
ahgittin/deserialize-empty-common-types
8eb5feee03 is described below

commit 8eb5feee037dff73b11f4d6ff14f7a55e3a61ef1
Author: Alex Heneveld <[email protected]>
AuthorDate: Wed May 11 17:22:52 2022 +0100

    fix gap in recent deserialization improvements to deserialize empty/default 
items
---
 .../resolve/jackson/CommonTypesSerialization.java     | 19 ++++++++++++++++++-
 ...rooklynRegisteredTypeJacksonSerializationTest.java |  8 ++++++++
 .../rest/util/json/BrooklynJacksonSerializerTest.java |  8 ++++++++
 3 files changed, 34 insertions(+), 1 deletion(-)

diff --git 
a/core/src/main/java/org/apache/brooklyn/core/resolve/jackson/CommonTypesSerialization.java
 
b/core/src/main/java/org/apache/brooklyn/core/resolve/jackson/CommonTypesSerialization.java
index 9a5e5ca796..ee39572c5b 100644
--- 
a/core/src/main/java/org/apache/brooklyn/core/resolve/jackson/CommonTypesSerialization.java
+++ 
b/core/src/main/java/org/apache/brooklyn/core/resolve/jackson/CommonTypesSerialization.java
@@ -36,6 +36,7 @@ import org.apache.brooklyn.util.time.Duration;
 import org.apache.brooklyn.util.time.Time;
 
 import java.io.IOException;
+import java.lang.reflect.Constructor;
 import java.time.Instant;
 import java.util.Date;
 import java.util.List;
@@ -87,6 +88,18 @@ public class CommonTypesSerialization {
         public abstract String convertObjectToString(T value, JsonGenerator 
gen, SerializerProvider provider) throws IOException;
         public abstract T convertStringToObject(String value, JsonParser p, 
DeserializationContext ctxt) throws IOException;
 
+        public T newEmptyInstance() {
+            try {
+                Class<T> t = getType();
+                Constructor<T> tc = t.getDeclaredConstructor();
+                tc.setAccessible(true);
+                return tc.newInstance();
+            } catch (Exception e) {
+                Exceptions.propagateIfFatal(e);
+                throw new IllegalArgumentException("Empty instances of " + 
getType() + " are not supported; provide a 'value:' indicating the value", e);
+            }
+        }
+
         public T convertSpecialMapToObject(Map value, JsonParser p, 
DeserializationContext ctxt) throws IOException {
             throw new IllegalStateException(getType()+" should be supplied as 
map with 'value'; instead had " + value);
         }
@@ -135,7 +148,8 @@ public class CommonTypesSerialization {
             @Override
             public T deserialize(JsonParser p, DeserializationContext ctxt) 
throws IOException, JsonProcessingException {
                 try {
-                    Object value = p.readValueAs(Object.class);
+                    Object valueO = p.readValueAs(Object.class);
+                    Object value = valueO;
                     if (value instanceof Map) {
                         if (((Map)value).size()==1 && 
((Map)value).containsKey(BrooklynJacksonSerializationUtils.VALUE)) {
                             value = ((Map) 
value).get(BrooklynJacksonSerializationUtils.VALUE);
@@ -145,9 +159,12 @@ public class CommonTypesSerialization {
                     }
 
                     if (value==null) {
+                        if (valueO==null) return newEmptyInstance();
                         return null;
                     } else if (value instanceof String || 
Boxing.isPrimitiveOrBoxedClass(value.getClass())) {
                         return convertStringToObject(value.toString(), p, 
ctxt);
+                    } else if (value instanceof Map) {
+                        return convertSpecialMapToObject((Map)value, p, ctxt);
                     } else {
                         throw new IllegalStateException(getType()+" should be 
supplied as string or map with 'type' and 'value'; instead had " + value);
                     }
diff --git 
a/core/src/test/java/org/apache/brooklyn/core/resolve/jackson/BrooklynRegisteredTypeJacksonSerializationTest.java
 
b/core/src/test/java/org/apache/brooklyn/core/resolve/jackson/BrooklynRegisteredTypeJacksonSerializationTest.java
index d8711380b3..84618e0670 100644
--- 
a/core/src/test/java/org/apache/brooklyn/core/resolve/jackson/BrooklynRegisteredTypeJacksonSerializationTest.java
+++ 
b/core/src/test/java/org/apache/brooklyn/core/resolve/jackson/BrooklynRegisteredTypeJacksonSerializationTest.java
@@ -31,6 +31,7 @@ import org.apache.brooklyn.core.typereg.RegisteredTypes;
 import org.apache.brooklyn.test.Asserts;
 import org.apache.brooklyn.util.collections.MutableList;
 import org.apache.brooklyn.util.core.config.ConfigBag;
+import org.apache.brooklyn.util.time.Duration;
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
@@ -148,4 +149,11 @@ public class 
BrooklynRegisteredTypeJacksonSerializationTest extends BrooklynMgmt
         Asserts.assertEquals(in.getAllConfig(), bag.getAllConfig());
     }
 
+    @Test
+    public void testEmptyDuration() {
+        Object impl = 
mgmt().getTypeRegistry().createBeanFromPlan(BeanWithTypePlanTransformer.FORMAT,
+                "type: "+ Duration.class.getName(), null, null);
+        Asserts.assertInstanceOf(impl, Duration.class);
+    }
+
 }
diff --git 
a/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/util/json/BrooklynJacksonSerializerTest.java
 
b/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/util/json/BrooklynJacksonSerializerTest.java
index a47a9b1694..9c51f84eba 100644
--- 
a/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/util/json/BrooklynJacksonSerializerTest.java
+++ 
b/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/util/json/BrooklynJacksonSerializerTest.java
@@ -222,6 +222,14 @@ public class BrooklynJacksonSerializerTest {
         Asserts.assertEquals(v2, Duration.minutes(90));
     }
 
+    @Test
+    public void testEmptyDurationCreation() throws JsonProcessingException {
+        ObjectMapper mapper = BeanWithTypeUtils.newYamlMapper(null, true, 
null, true);
+
+        Object v2 = mapper.readerFor(Object.class).readValue(  "type: 
"+StringEscapes.JavaStringEscapes.wrapJavaString(Duration.class.getName()) );
+        Asserts.assertEquals(v2, Duration.of(0));
+    }
+
     @Test
     public void testManagementContextReadWrite() throws 
JsonProcessingException {
         ManagementContext mgmt = null;

Reply via email to