Should never call `set()` on setterless property during deserialization

193 views
Skip to first unread message

luca boncompagni

unread,
Apr 19, 2020, 7:58:43 PM4/19/20
to jackson-user
Hi,
I have something like the following code:

@JsonIgnoreProperties(ignoreUnknown = true)
public class DataBean {

 
private final String val;

 
@JsonCreator
 
public DataBean(@JsonProperty(value = "val"String val) {
                 this.val = val;
 }

 
public String getVal() {
 
                return val;
 }
 
 
public List<String> getList(){
 
                return new ArrayList<>();
 }


 
@Override
 
public String toString() {
 
                return "DataBean [val=" + val + "]";
 }
 
public static void main(String[] args) throws Exception {
 
                ObjectMapper om = new ObjectMapper();
                 String json;
                 DataBean out;
                  json = "{\"list\":[\"11\"],\"val\":\"VAL2\"}";
                 out = om.readerFor(DataBean.class).readValue(json);
                 System.out.println("this is ko" + out);
 }
}

When I run the test I get: "java.lang.UnsupportedOperationException: Should never call `set()` on setterless property ('list')". If I modify the JSON, having the value of the constructor as first field, it runs OK. 

         json = "{\"val\":\"VAL2\",\"list\":[\"11\"]}";
         
out = om.readerFor(DataBean.class).readValue(json);
         
System.out.println("this is ok: " + out);

I know that I can solve it setting MapperFeature.USE_GETTERS_AS_SETTERS to false, but I can't. 

In order to solve the issue, I try the following orrible patch:

diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/SetterlessProperty.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/SetterlessProperty.java
index 9c80e49..ca7ec85 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/impl/SetterlessProperty.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/SetterlessProperty.java
@@ -3,6 +3,8 @@
 import java.io.IOException;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
+import java.util.Collection;
+import java.util.List;
 
 import com.fasterxml.jackson.core.JsonParser;
 import com.fasterxml.jackson.core.JsonToken;
@@ -144,7 +146,21 @@
 
     @Override
     public final void set(Object instance, Object value) throws IOException {
-        throw new UnsupportedOperationException("Should never call `set()` on setterless property ('"+getName()+"')");
+    if(value instanceof Collection) {
+   
+    Collection<Object> collection = (Collection<Object>) value;
+ Collection<Object> toModify;
+    try {
+    toModify = (Collection<Object> )_getter.invoke(instance, (Object[]) null);
+    toModify.addAll(collection);
+    } catch (Exception e) {
+    _throwAsIOE((JsonParser)null, e);
+    return; // never gets here
+    }
+   
+    } else {
+    throw new UnsupportedOperationException("Should never call `set()` on setterless property ('"+getName()+"')");
+    }
     }
 
     @Override+    Collection<Object> collection = (Collection<Object>) value;

Is there any other way or patch to solve it?

Thanks,
Luca 

Tatu Saloranta

unread,
Apr 19, 2020, 8:01:50 PM4/19/20
to jackson-user
Hi there! I suggest you file a github issue for `jackson-databind`,
with all information from above.
I can't say if it is a bug without digging deeper, and right now it
may take a while for me to find time.
But once I have time I'll have a look and hopefully we can resolve the
issue, whatever it is.

-+ Tatu +-
Reply all
Reply to author
Forward
0 new messages