jackson-databind: Java Record `@JsonAnySetter` value is null after deserialization
Describe the bug
When deserializing a Java Record with @JsonAnySetter annotated field the field is left as null and the unmapped values are ignored. Given the nature of Java records, there is no other way to get the unmapped fields values (like in case of annotating a setter method).
Version information 2.13.2.2
To Reproduce
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class JsonAnySetterRecordTest {
record TestRecord(
@JsonProperty String field,
@JsonAnySetter Map<String, Object> anySetter
) {}
@Test
void testJsonAnySetterOnRecord() throws JsonProcessingException {
var json = """
{
"field": "value",
"unmapped1": "value1",
"unmapped2": "value2"
}
""";
var objectMapper = new ObjectMapper();
var deserialized = objectMapper.readValue(json, TestRecord.class);
assertEquals("value", deserialized.field());
assertEquals(Map.of("unmapped1", "value1", "unmapped2", "value2"), deserialized.anySetter());
}
}
Running this test the result will be:
Expected :{unmapped1=value1, unmapped2=value2}
Actual :null
Expected behavior
The @JsonAnySetter annotated field should contain a Map instance with all unmapped fields and their values.
Additional context
The problem happens in com.fasterxml.jackson.databind.deser.SettableAnyProperty class in method set(...). The value of the @JsonAnySetter annotated field is null and therefore setting the property value is skipped.
The suggested solution would be for Java Record to provide a new empty Map instance for the annotated field to gather the unmapped properties and this would then be provided to Record’s constructor when the deserialization is concluded. Given the immutable nature of Java Records, this should ideally be some kind of immutable Map implementation (i.e. Map.copyOf(...) ?)
There might be a workaround (which however won’t be probably feasible in all cases) by supplying an additional @JsonCreator constructor or factory method, where the @JsonAnySetter field is initialized to an empty map and then to ensure the immutability, the getter for this map needs to be overridden and the value returned wrapped as immutable.
record TestRecord(
String field,
@JsonAnySetter Map<String, Object> anySetter
) {
@JsonCreator
TestRecord(@JsonProperty("field") String field) {
this(field, new HashMap<>());
}
public Map<String, Object> anySetter() {
return Collections.unmodifiableMap(anySetter);
}
}
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 8
- Comments: 15 (10 by maintainers)
Commits related to this issue
- Add Records+JsonAnySetter workarounds shared in #3439, as test cases. — committed to yihtserns/jackson-databind by yihtserns 2 years ago
- Add Records+JsonAnySetter workarounds shared in #3439, as test cases. — committed to yihtserns/jackson-databind by yihtserns 2 years ago
- Add Records+JsonAnySetter workarounds shared in #3439, as test cases. — committed to yihtserns/jackson-databind by yihtserns 2 years ago
- Add test for, mark as fixed, #3439 — committed to FasterXML/jackson-databind by cowtowncoder 2 years ago
@kleino JsonAnySetter documents two ways:
You’ve been using #1, which is no longer usable because #3737 ignores Record fields for deserialization (so Jackson cannot “see” the
@JsonAnySetteron the now-gone field).You can still use #2:
Thans for the hint @oujesky, I’ve managed to make it work:
AnySetterOnCreatorJacksonModuleNow I can write just
and it does what I wanted 🚀 I have yet to fix the immutability problem, but that shouldn’t be much hard to hack 😃
Just came across this issue, because I ran into it as well, but I wanted to share my workaround, which is similar to the one provided but uses a compact constructor to avoid the need for a “full constuctor” annotated with JsonCreator that replicates the entire arglist. This doesn’t remove the fact that we’re adding a mutable map to the record instance, which might be undesirable in some cases.
@lordvlad If someone was working on this, they’d likely add a note. So I doubt anyone is working on it. I do not have bandwidth myself, but perhaps @JooHyukKim might have?