Skip to content

@JsonIncludeProperties, @JsonIgnoreProperties ignored when serializing Records, if there is getter override #4630

@yihtserns

Description

@yihtserns

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

A Record component whose accessor method is overridden and:

...will still get serialized into the generated JSON.

Version Information

Since 2.15.0.

Reproduction

For @JsonIncludeProperties

record Id2Name(int id, String name) {
}
record RecordWithJsonIncludeProperties(@JsonIncludeProperties("id") Id2Name child) {
    @Override
    public Id2Name child() {
        return child;
    }
}

String json = new ObjectMapper().writeValueAsString(new RecordWithJsonIncludeProperties(new Id2Name(123, "Bob")));
// failed with:
// org.opentest4j.AssertionFailedError:
// Expected :{"child":{"id":123}}
// Actual   :{"child":{"id":123,"name":"Bob"}}
assertEquals("{\"child\":{\"id\":123}}", json);

For @JsonIgnoreProperties

record Id2Name(int id, String name) {
}
record RecordWithJsonIgnoreProperties(@JsonIgnoreProperties("name") Id2Name child) {
    @Override
    public Id2Name child() {
        return child;
    }
}

String json = new ObjectMapper().writeValueAsString(new RecordWithJsonIgnoreProperties(new Id2Name(123, "Bob")));
// failed with:
// org.opentest4j.AssertionFailedError:
// Expected :{"child":{"id":123}}
// Actual   :{"child":{"id":123,"name":"Bob"}}
assertEquals("{\"child\":{\"id\":123}}", json);

Expected behavior

No response

Additional context

NOTE: I'm not personally affected by this, just something I found when testing for #4626.

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.18Issues planned at 2.18 or laterRecordIssue related to JDK17 java.lang.Record support

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions