Skip to content

@JsonIgnore and @JsonProperty.access=READ_ONLY on Record property ignored for deserialization #4628

@yihtserns

Description

@yihtserns

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

When a @JsonProperty.value is given the same value as the property it is annotated on, @JsonProperty.access=READ_ONLY seems to be ignored during deserialization.

Version Information

Oldest I tested this with is 2.14.2, latest is 2.18.

Reproduction

For @JsonIgnore

record RecordWithJsonIgnoreNamedProperty(int id,
                                         // Note that @JsonProperty.value == "xxx", which has the same name as the Record component `xxx`
                                         @JsonIgnore @JsonProperty(value = "xxx") String xxx) {
}

RecordWithJsonIgnoreNamedProperty deser = new ObjectMapper().readValue("{\"id\":123,\"xxx\":\"Bob\"}", RecordWithJsonIgnoreNamedProperty.class);

// failed with:
// org.opentest4j.AssertionFailedError:
// Expected :RecordWithJsonIgnoreNamedProperty[id=123, xxx=null]
// Actual   :RecordWithJsonIgnoreNamedProperty[id=123, xxx=Bob]
assertEquals(new RecordWithJsonIgnoreNamedProperty(123, null), deser);

For @JsonProperty.access=READ_ONLY

record RecordWithReadOnlyNamedProperty(int id,
                                       // Note that @JsonProperty.value == "xxx", which has the same name as the Record component `xxx` 
                                       @JsonProperty(value = "xxx", access = JsonProperty.Access.READ_ONLY) String xxx) {
}

RecordWithReadOnlyNamedProperty deser = new ObjectMapper().readValue("{\"id\":123,\"xxx\":\"Bob\"}", RecordWithReadOnlyNamedProperty.class);

// failed with:
// org.opentest4j.AssertionFailedError: 
// Expected :RecordWithReadOnlyNamedProperty[id=123, xxx=null]
// Actual   :RecordWithReadOnlyNamedProperty[id=123, xxx=Bob]
assertEquals(new RecordWithReadOnlyNamedProperty(123, null), deser);

Expected behavior

No response

Additional context

NOTE: Seems to be an edge case that user wouldn't/shouldn't encounter. Not reproducible for non-Record classes. I'm not personally affected by this, just something I found when working on #4624:

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