You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2021/05/07 09:42:32 UTC

[brooklyn-server] branch master updated: minor deserialization/coercion fixes

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

heneveld 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 685a2bc  minor deserialization/coercion fixes
685a2bc is described below

commit 685a2bc56d957abe4e52e279dfcaca6008b84d82
Author: Alex Heneveld <al...@cloudsoftcorp.com>
AuthorDate: Fri May 7 10:41:17 2021 +0100

    minor deserialization/coercion fixes
    
    fix deserialization of type property in bean and treat lists supplied for sets as a collection to skip coercion
---
 .../brooklyn/core/config/internal/AbstractConfigMapImpl.java | 12 +++++++++++-
 .../brooklyn/core/resolve/jackson/AsPropertyIfAmbiguous.java |  9 +++++++--
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/core/src/main/java/org/apache/brooklyn/core/config/internal/AbstractConfigMapImpl.java b/core/src/main/java/org/apache/brooklyn/core/config/internal/AbstractConfigMapImpl.java
index 2f4623f..f9e8536 100644
--- a/core/src/main/java/org/apache/brooklyn/core/config/internal/AbstractConfigMapImpl.java
+++ b/core/src/main/java/org/apache/brooklyn/core/config/internal/AbstractConfigMapImpl.java
@@ -285,7 +285,7 @@ public abstract class AbstractConfigMapImpl<TContainer extends BrooklynObject> i
         } else if (key instanceof StructuredConfigKey) {
             // no coercion for these structures (they decide what to do)
             return v;
-        } else if ((v instanceof Map || v instanceof Iterable) && key.getType().isInstance(v)) {
+        } else if ((v instanceof Map || v instanceof Iterable) && isStructurallyCompatible(key, v)) {
             // don't do coercion on put for these, if the key type is compatible, 
             // because that will force resolution deeply
             return v;
@@ -303,6 +303,16 @@ public abstract class AbstractConfigMapImpl<TContainer extends BrooklynObject> i
         }
     }
 
+    private <T> boolean isStructurallyCompatible(ConfigKey<T> key, Object v) {
+        if (key.getType().isInstance(v)) return true;
+        if (Collection.class.isAssignableFrom(key.getType())) {
+            if (v instanceof Iterable) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     @Override
     public Map<String,Object> asMapWithStringKeys() {
         return mapViewWithStringKeys;
diff --git a/core/src/main/java/org/apache/brooklyn/core/resolve/jackson/AsPropertyIfAmbiguous.java b/core/src/main/java/org/apache/brooklyn/core/resolve/jackson/AsPropertyIfAmbiguous.java
index 26fc860..ecf1ab3 100644
--- a/core/src/main/java/org/apache/brooklyn/core/resolve/jackson/AsPropertyIfAmbiguous.java
+++ b/core/src/main/java/org/apache/brooklyn/core/resolve/jackson/AsPropertyIfAmbiguous.java
@@ -33,6 +33,7 @@ import com.fasterxml.jackson.databind.jsontype.TypeIdResolver;
 import com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer;
 import com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeSerializer;
 import com.fasterxml.jackson.databind.util.TokenBuffer;
+import java.lang.reflect.Method;
 import java.util.function.Supplier;
 import org.apache.brooklyn.core.resolve.jackson.AsPropertyIfAmbiguous.HasBaseType;
 import org.apache.brooklyn.util.exceptions.Exceptions;
@@ -42,6 +43,7 @@ import java.io.IOException;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import org.apache.brooklyn.util.text.Strings;
 
 public class AsPropertyIfAmbiguous {
     public interface HasBaseType {
@@ -122,12 +124,15 @@ public class AsPropertyIfAmbiguous {
         @Override
         public Object deserializeTypedFromObject(JsonParser p, DeserializationContext ctxt) throws IOException {
             if (_idResolver instanceof HasBaseType) {
-                if (Reflections.findFieldMaybe(((HasBaseType)_idResolver).getBaseType().getRawClass(), _typePropertyName).isPresent()) {
+                if (// object has field with same name as the type property - don't treat the type property supplied here as the type
+                        Reflections.findFieldMaybe(((HasBaseType)_idResolver).getBaseType().getRawClass(), _typePropertyName).isPresent()
+                        || // or object has getter with same name as the type property
+                        Reflections.findMethodMaybe(((HasBaseType)_idResolver).getBaseType().getRawClass(), "get"+ Strings.toInitialCapOnly(_typePropertyName)).isPresent()
+                ) {
                     // don't read type id, just deserialize
                     JsonDeserializer<Object> deser = ctxt.findContextualValueDeserializer(((HasBaseType)_idResolver).getBaseType(), _property);
                     return deser.deserialize(p, ctxt);
                 }
-
                 // TODO MapperFeature.USE_BASE_TYPE_AS_DEFAULT_IMPL should do this
                 if (!Objects.equals(_defaultImpl, ((HasBaseType) _idResolver).getBaseType())) {
                     AsPropertyButNotIfFieldConflictTypeDeserializer delegate = new AsPropertyButNotIfFieldConflictTypeDeserializer(_baseType, _idResolver, _typePropertyName, _typeIdVisible, ((HasBaseType) _idResolver).getBaseType(), _inclusion);