jackson-databind icon indicating copy to clipboard operation
jackson-databind copied to clipboard

@JsonIgnore & @JsonProperty.access=READ_ONLY on Record property ignored for deserialization, if @JsonProperty.value is the same as the property name

Open yihtserns opened this issue 1 year ago • 0 comments

Search before asking

  • [X] 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:

  • For @JsonIgnore: https://github.com/yihtserns/jackson-databind/blob/8c14e0916647128082cd8739d7e87e3884c1aa5e/src/test-jdk17/java/com/fasterxml/jackson/databind/records/RecordWithJsonIgnoreTest.java#L64-L68
  • For @JsonProperty.access=READ_ONLY: https://github.com/yihtserns/jackson-databind/blob/8c14e0916647128082cd8739d7e87e3884c1aa5e/src/test-jdk17/java/com/fasterxml/jackson/databind/records/RecordWithReadOnlyTest.java#L85-L93

yihtserns avatar Jul 21 '24 16:07 yihtserns