FasterXML / jackson-databind

General data-binding package for Jackson (2.x): works on streaming API (core) implementation(s)
Apache License 2.0
3.51k stars 1.37k forks source link

Cannot ignore @JsonProperty annotated property serialization in subclass #2350

Open ogmios-voice opened 5 years ago

ogmios-voice commented 5 years ago

Tested against versions 2.9.8 and 2.9.9: the serialization of BwoProp returns {"b":2} while in case of BwProp the result is {"b":2,"a":1}.

@Test
public void ser() throws Exception {
    final ObjectMapper mapr = new ObjectMapper();
    final String jsonWProp  = mapr.writer().writeValueAsString(new BwProp());
    final String jsonWoProp = mapr.writer().writeValueAsString(new BwoProp());
    Assert.assertEquals(jsonWoProp, jsonWProp);
}

static class AwProp {
    @JsonProperty("a")
    protected int a = 1;

    public int getA() {
        return a;
    }
}

static class BwProp extends AwProp {
    int b = 2;

    @Override
    @JsonIgnore
    public int getA() {
        return super.getA();
    }

    public int getB() {
        return b;
    }
}

static class AwoProp {
    protected int a = 1;

    public int getA() {
        return a;
    }
}

static class BwoProp extends AwoProp {
    int b = 2;

    @Override
    @JsonIgnore
    public int getA() {
        return super.getA();
    }

    public int getB() {
        return b;
    }
}
cowtowncoder commented 5 years ago

That's the way it works: if you have @JsonProperty in one accessor (say, field), @JsonIgnore in another (say ,method), usage is considered "split". You will need to either: