jackson-databind: @JsonPOJOBuilder doesn't work with @JsonAnySetter
Search before asking
- I searched in the issues and found nothing similar.
Describe the bug
It seems like @JsonPOJOBuilder doesn’t play nice with @JsonAnySetter. In the following example, the virtualized property "attributes’ (which should contain all properties not specified in the POJO), is null when deserialized, even though additional properties exist in the input JSON.
Version Information
2.18.4/2.19.0/2.19.1
Reproduction
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class TestTest {
@Test
public void test() throws JsonProcessingException {
final ObjectMapper mapper = new ObjectMapper();
final String json = "{\"name\":\"name\",\"items\":[{\"type\":\"type\",\"key1\":\"value1\",\"key2\":\"value2\"}]}";
final CollectionType serialized = mapper.readValue(json, CollectionType.class);
assertNotNull(serialized.getItems().get(0).getAttributes());
assertEquals("value1", serialized.getItems().get(0).getAttributes().get("key1"));
assertEquals("value2", serialized.getItems().get(0).getAttributes().get("key2"));
}
@com.fasterxml.jackson.databind.annotation.JsonDeserialize(builder = TestTest.CollectionType.CollectionTypeBuilder.class)
public static class CollectionType {
private String name;
private List<Item> items;
@com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder(withPrefix = "", buildMethodName = "build")
public static class CollectionTypeBuilder {
private String name;
private List<Item> items;
CollectionTypeBuilder() {
}
/**
* @return {@code this}.
*/
public TestTest.CollectionType.CollectionTypeBuilder name(final String name) {
this.name = name;
return this;
}
/**
* @return {@code this}.
*/
public TestTest.CollectionType.CollectionTypeBuilder items(final List<Item> items) {
this.items = items;
return this;
}
public TestTest.CollectionType build() {
return new TestTest.CollectionType(this.name, this.items);
}
@Override
public java.lang.String toString() {
return "TestTest.CollectionType.CollectionTypeBuilder(name=" + this.name + ", items=" + this.items + ")";
}
}
public static TestTest.CollectionType.CollectionTypeBuilder builder() {
return new TestTest.CollectionType.CollectionTypeBuilder();
}
public String getName() {
return this.name;
}
public List<Item> getItems() {
return this.items;
}
public void setName(final String name) {
this.name = name;
}
public void setItems(final List<Item> items) {
this.items = items;
}
@Override
public boolean equals(final java.lang.Object o) {
if (o == this) return true;
if (!(o instanceof TestTest.CollectionType)) return false;
final TestTest.CollectionType other = (TestTest.CollectionType) o;
if (!other.canEqual((java.lang.Object) this)) return false;
final java.lang.Object this$name = this.getName();
final java.lang.Object other$name = other.getName();
if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
final java.lang.Object this$items = this.getItems();
final java.lang.Object other$items = other.getItems();
if (this$items == null ? other$items != null : !this$items.equals(other$items)) return false;
return true;
}
protected boolean canEqual(final java.lang.Object other) {
return other instanceof TestTest.CollectionType;
}
@Override
public int hashCode() {
final int PRIME = 59;
int result = 1;
final java.lang.Object $name = this.getName();
result = result * PRIME + ($name == null ? 43 : $name.hashCode());
final java.lang.Object $items = this.getItems();
result = result * PRIME + ($items == null ? 43 : $items.hashCode());
return result;
}
@Override
public java.lang.String toString() {
return "TestTest.CollectionType(name=" + this.getName() + ", items=" + this.getItems() + ")";
}
public CollectionType() {
}
public CollectionType(final String name, final List<Item> items) {
this.name = name;
this.items = items;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
@com.fasterxml.jackson.databind.annotation.JsonDeserialize(builder = TestTest.Item.ItemBuilder.class)
public static class Item {
private String type;
@JsonAnyGetter
@JsonAnySetter
private Map<String, Object> attributes;
@JsonIgnoreProperties(ignoreUnknown = true)
@com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder(withPrefix = "", buildMethodName = "build")
public static class ItemBuilder {
private String type;
private Map<String, Object> attributes;
ItemBuilder() {
}
/**
* @return {@code this}.
*/
public TestTest.Item.ItemBuilder type(final String type) {
this.type = type;
return this;
}
/**
* @return {@code this}.
*/
public TestTest.Item.ItemBuilder attributes(final Map<String, Object> attributes) {
this.attributes = attributes;
return this;
}
public TestTest.Item build() {
return new TestTest.Item(this.type, this.attributes);
}
@Override
public java.lang.String toString() {
return "TestTest.Item.ItemBuilder(type=" + this.type + ", attributes=" + this.attributes + ")";
}
}
public static TestTest.Item.ItemBuilder builder() {
return new TestTest.Item.ItemBuilder();
}
public String getType() {
return this.type;
}
public Map<String, Object> getAttributes() {
return this.attributes;
}
public void setType(final String type) {
this.type = type;
}
public void setAttributes(final Map<String, Object> attributes) {
this.attributes = attributes;
}
@Override
public boolean equals(final java.lang.Object o) {
if (o == this) return true;
if (!(o instanceof TestTest.Item)) return false;
final TestTest.Item other = (TestTest.Item) o;
if (!other.canEqual((java.lang.Object) this)) return false;
final java.lang.Object this$type = this.getType();
final java.lang.Object other$type = other.getType();
if (this$type == null ? other$type != null : !this$type.equals(other$type)) return false;
final java.lang.Object this$attributes = this.getAttributes();
final java.lang.Object other$attributes = other.getAttributes();
if (this$attributes == null ? other$attributes != null : !this$attributes.equals(other$attributes)) return false;
return true;
}
protected boolean canEqual(final java.lang.Object other) {
return other instanceof TestTest.Item;
}
@Override
public int hashCode() {
final int PRIME = 59;
int result = 1;
final java.lang.Object $type = this.getType();
result = result * PRIME + ($type == null ? 43 : $type.hashCode());
final java.lang.Object $attributes = this.getAttributes();
result = result * PRIME + ($attributes == null ? 43 : $attributes.hashCode());
return result;
}
@Override
public java.lang.String toString() {
return "TestTest.Item(type=" + this.getType() + ", attributes=" + this.getAttributes() + ")";
}
public Item() {
}
public Item(final String type, final Map<String, Object> attributes) {
this.type = type;
this.attributes = attributes;
}
}
}
Expected behavior
The additional properties are deserialized
If the following is replaced, it works:
@JsonIgnoreProperties(ignoreUnknown = true)
@com.fasterxml.jackson.databind.annotation.JsonDeserialize(builder = TestTest.Item.ItemBuilder.class)
public static class Item {
private String type;
@JsonAnyGetter
@JsonAnySetter
private Map<String, Object> attributes;
@JsonIgnoreProperties(ignoreUnknown = true)
@com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder(withPrefix = "", buildMethodName = "build")
public static class ItemBuilder {
with
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Item {
private String type;
@JsonAnyGetter
@JsonAnySetter
private Map<String, Object> attributes;
public static class ItemBuilder {
Additional context
No response
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 15 (13 by maintainers)
@pjfanning That is correct, the
lombok.Generatedis just a marker annotation, but regardless, I’ve removed every third party dependency (except JUnit). The issue is still presenting.