Skip to content
This repository was archived by the owner on Nov 7, 2019. It is now read-only.

@JsonIgnore not preserved on static setter in an abstract class #25

@Maxouwell

Description

@Maxouwell

When deserialising, the MrBean module seems to not preserve the @JsonIgnore Annotation.

Test code following :

public class IgnoreBugTest {

    public static final ObjectMapper mapper = new ObjectMapper()
            .registerModule(new MrBeanModule())
            .enable(SerializationFeature.INDENT_OUTPUT);

    public AbstractRoot root;

    public IgnoreBugTest() throws JsonParseException, JsonMappingException, IOException {}

    public void setRoot(AbstractRoot root) {
        this.root = root;
    }

    public AbstractRoot getRoot() {
        return root;
    }

    public static void main(String[] args) throws Exception {
        IgnoreBugTest ibt = new IgnoreBugTest();

        Root root = new Root();
        root.setX(2);
        root.setY(3);

        ibt.setRoot(root);

        String jsonValue = mapper.writeValueAsString(ibt);
        System.out.println(jsonValue);
        System.out.println(mapper.readValue(jsonValue, IgnoreBugTest.class));
    }

}

public class Root extends AbstractRoot {

    private int y = 0;

    public Root() {}

    public void setY(int y) {
        this.y = y;
    }

    public int getY() {
        return y;
    }
}

@JsonTypeInfo(use=JsonTypeInfo.Id.MINIMAL_CLASS, include=JsonTypeInfo.As.PROPERTY)
public abstract class AbstractRoot {

    private static Leaf leaf;

    private int x = 0;

    public AbstractRoot() {}

    @JsonIgnore
    public static void setLeaf(Leaf leaf) {
        AbstractRoot.leaf = leaf;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

}

//@JsonIgnoreProperties(value={"leaf"} ,ignoreUnknown=true)
public abstract class AbstractRoot {

    private static Leaf leaf;

    private int x = 0;

    public AbstractRoot() {}

    @JsonIgnore
    public static void setLeaf(Leaf leaf) {
        AbstractRoot.leaf = leaf;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

}

//Bad jackson class, to generate the IllegalArgumentException
//This class should be ignored by Jackson
public class Leaf {

    public String test;


    public void setTest(String test) {
        this.test = test;
    }

    public void setTest(StringBuilder test) {
        this.test = test.toString();
    }
}

This code fail on deserialization, because it's try to check the Leaf class, which should be ignored.

Caused by: java.lang.IllegalArgumentException: Conflicting setter definitions for property "test": org.l6m.Leaf#setTest(1 params) vs org.l6m.Leaf#setTest(1 params)
    at com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder.getSetter(POJOPropertyBuilder.java:293)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.filterBeanProps(BeanDeserializerFactory.java:583)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.addBeanProps(BeanDeserializerFactory.java:479)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:220)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:143)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:409)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:358)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:265)
    ... 19 more

Jackson does not take into account the Leaf class if :

  • The abstract class is annoted with @JsonIgnoreProperties, with the leaf property as value
  • If the class used for the root property in IgnoreBugTest is Root and not AbstractRoot

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions