On Sun, Apr 19, 2020 at 4:58 PM luca boncompagni <[email protected]> wrote:
>
> 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

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 +-

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jackson-user/CAL4a10jO5jq%3Dw_SYiABL_%3DVf2yabAAtg-0Dsao7Z_FDA2knssg%40mail.gmail.com.

Reply via email to