Jackson Databind Should never call set() on setterless property during deserialization
2025-08-13 14:05:09
5454
Hi,
I have something like the following code:
```
public class DataBean {
private final String val;
@JsonCreator
public DataBean(
@JsonProperty(value = "val")
String val) {
super();
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:
```
Exception in thread "main" com.fasterxml.jackson.databind.exc.ValueInstantiationException: Cannot construct instance of `DataBean`, problem: Should never call `set()` on setterless property ('list')
at [Source: (String)"{"list":["11"],"val":"VAL2"}"; line: 1, column: 28]
at com.fasterxml.jackson.databind.exc.ValueInstantiationException.from(ValueInstantiationException.java:47)
at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1735)
at com.fasterxml.jackson.databind.DeserializationContext.handleInstantiationProblem(DeserializationContext.java:1109)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.wrapInstantiationProblem(BeanDeserializerBase.java:1757)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:424)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1287)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1719)
at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1261)
at DataBean.main(DataBean.java:53)
Caused by: java.lang.UnsupportedOperationException: Should never call `set()` on setterless property ('list')
at com.fasterxml.jackson.databind.deser.impl.SetterlessProperty.set(SetterlessProperty.java:147)
at com.fasterxml.jackson.databind.deser.impl.PropertyValue$Regular.assign(PropertyValue.java:62)
at com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator.build(PropertyBasedCreator.java:207)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:422)
```
If I modify the JSON, having the value of the constructor as first field, it runs OK.
In order to solve the issue, I try the following horrible patch:
The problems comes from need to buffer content in case where values needed by @JsonCreator are not all available before other properties (ones not passed via constructor); this is why ordering matters wrt failure.
But problem specifically is that whereas "setterless" properties can be deserialized directly into List value obtained via getter, buffering is done by actually deserializing new value. But this new value can not be assigned due there not being setter method.
Conceptually this could be handled by instead buffering raw sequence of tokens (which is technically possible), but is not done here (ideally such buffering is avoided).
I'll have to think about possible way to make this work: it is perfectly reasonable for users to expect it to work (to the degree "setterless" properties make sense in general).
Comment From: debalindas
Facing the same issue.
Comment From: cowtowncoder
Appears to work on 3.0?
Comment From: cowtowncoder
Ah. Only due to change of MapperFeature.USE_GETTERS_AS_SETTERS defaults; adding that setting re-introduces failure.
Comment From: cowtowncoder
Whoa! Fixing #5237 allowed fixing this one, too. Will check in the fix soon.
Comment From: cowtowncoder
Took 5 years but finally fixed; will be in 2.20.0!