Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@JsonIgnore does not if together with @JsonProperty or @JsonFormat #3357

Closed
lizongbo opened this issue Dec 21, 2021 · 2 comments
Closed

@JsonIgnore does not if together with @JsonProperty or @JsonFormat #3357

lizongbo opened this issue Dec 21, 2021 · 2 comments
Labels
has-failing-test Indicates that there exists a test case (under `failing/`) to reproduce the issue
Milestone

Comments

@lizongbo
Copy link

lizongbo commented Dec 21, 2021

Describe the bug
JsonIgnore is not work when with JsonProperty or JsonFormat。
code:

    @JsonIgnore
    @JsonProperty("create_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    Date createTime;

Version information
2.13.1

To Reproduce
test case:

@Test
void testJsonIgnore() throws JsonProcessingException {
    TestIgnorebean tb = new TestIgnorebean();
    tb.setCreateTime(new Date());
    tb.setAge(10);
    tb.setNickName("lizongbo");
    ObjectMapper mapper = new ObjectMapper();
    JsonInclude.Value full = JsonInclude.Value.construct(Include.ALWAYS, Include.ALWAYS);
    mapper.setDefaultPropertyInclusion(full);
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    mapper.enable(Feature.WRITE_BIGDECIMAL_AS_PLAIN);
    mapper.setTimeZone(TimeZone.getDefault());
    System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(tb));
    System.out.println("jackson vesion " + JsonIgnore.class.getPackage().getImplementationVersion());
}

static class TestIgnorebean {
    @JsonIgnore
    @JsonProperty("nick_name")
    String nickName;
    @JsonIgnore
    @JsonProperty("create_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    Date createTime;
    int age;

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}

result:

{
  "nickName" : "lizongbo",
  "createTime" : 1640089093927,
  "age" : 10
}

jackson vesion 2.13.1

// JsonIgnore, JsonFormat and JsonProperty all not work correct.

Expected behavior
need:

{
  "age" : 10
}

jackson vesion 2.13.1

@lizongbo lizongbo added the to-evaluate Issue that has been received but not yet evaluated label Dec 21, 2021
@cowtowncoder
Copy link
Member

Hmmh. I am not sure if this is working properly or not: combination of @JsonIgnore (to ignore) and @JsonProperty (to explicitly include) is undefined. You should NOT add @JsonProperty for anything being ignored since that is specifically meant to mean "DO INCLUDE". This is not true for @JsonFormat which should not have any effect if @JsonIgnore exists.

I can see why it might be expected that @JsonIgnore has precedence: I hope to have time to investigate this to make sure I understand what is going on.

@cowtowncoder
Copy link
Member

Hmmmh. Interesting. At first I was unable to reproduce this, with simpler test like:

    static class IgnoreAndProperty3357 {
        public int toInclude = 2;

        @JsonIgnore
        @JsonProperty
        @JsonFormat
        public int toIgnore = 3;

in which @JsonIgnore takes effect over @JsonProperty.

But if I make field non-public, leave annotations, add a public getter, then the problem is reproduced.

On short term I would recommend removing unhelpful @JsonProperty -- it really should NOT be there -- or moving annotations to getter (or even setter method). Any of these should make behavior ignore property in question.

I will however add a failing test for this problem since I do think that ideally even this combination should result in ignoral of the property.

cowtowncoder added a commit that referenced this issue Dec 28, 2021
@cowtowncoder cowtowncoder added has-failing-test Indicates that there exists a test case (under `failing/`) to reproduce the issue and removed to-evaluate Issue that has been received but not yet evaluated labels Dec 28, 2021
@cowtowncoder cowtowncoder changed the title JsonIgnore is not work when with JsonProperty or JsonFormat @JsonIgnore does not if together with @JsonProperty or @JsonFormat May 17, 2022
@cowtowncoder cowtowncoder added this to the 2.14.0 milestone May 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has-failing-test Indicates that there exists a test case (under `failing/`) to reproduce the issue
Projects
None yet
Development

No branches or pull requests

2 participants