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 b00424b tweak to how we determine type when deserializing
b00424b is described below
commit b00424b15046af7931feb4fb07af3cf1a1062d95
Author: Alex Heneveld <[email protected]>
AuthorDate: Mon May 10 11:00:21 2021 +0100
tweak to how we determine type when deserializing
normally we treat a json field 'type' as what type should be deserialized,
not a field in the class to set;
but originally we did the latter if there is a field called 'type';
and recently we added an "or if there is a method called 'getType';
now we qualify those exceptions to say "only if it isn't json ignored"
---
.../core/resolve/jackson/AsPropertyIfAmbiguous.java | 16 ++++++++++++++--
1 file changed, 14 insertions(+), 2 deletions(-)
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 ecf1ab3..10a7bcb 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
@@ -18,6 +18,7 @@
*/
package org.apache.brooklyn.core.resolve.jackson;
+import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
@@ -33,10 +34,13 @@ 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.AccessibleObject;
+import java.lang.reflect.Field;
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;
+import org.apache.brooklyn.util.guava.Maybe;
import org.apache.brooklyn.util.javalang.Reflections;
import java.io.IOException;
@@ -125,9 +129,9 @@ public class AsPropertyIfAmbiguous {
public Object deserializeTypedFromObject(JsonParser p,
DeserializationContext ctxt) throws IOException {
if (_idResolver instanceof HasBaseType) {
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()
+
presentAndNotJsonIgnored(Reflections.findFieldMaybe(((HasBaseType)_idResolver).getBaseType().getRawClass(),
_typePropertyName))
|| // or object has getter with same name as the type
property
-
Reflections.findMethodMaybe(((HasBaseType)_idResolver).getBaseType().getRawClass(),
"get"+ Strings.toInitialCapOnly(_typePropertyName)).isPresent()
+
presentAndNotJsonIgnored(Reflections.findMethodMaybe(((HasBaseType)_idResolver).getBaseType().getRawClass(),
"get"+ Strings.toInitialCapOnly(_typePropertyName)))
) {
// don't read type id, just deserialize
JsonDeserializer<Object> deser =
ctxt.findContextualValueDeserializer(((HasBaseType)_idResolver).getBaseType(),
_property);
@@ -142,6 +146,14 @@ public class AsPropertyIfAmbiguous {
return super.deserializeTypedFromObject(p, ctxt);
}
+ private boolean presentAndNotJsonIgnored(Maybe<? extends
AccessibleObject> fm) {
+ if (!fm.isPresent()) return false;
+ AccessibleObject f = fm.get();
+ JsonIgnore ignored = f.getAnnotation(JsonIgnore.class);
+ if (ignored!=null) return false;
+ return true;
+ }
+
@Override
public TypeDeserializer forProperty(BeanProperty prop) {
return (prop == _property) ? this : new
AsPropertyButNotIfFieldConflictTypeDeserializer(this, prop);