Skip to content

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

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

Closed
1 task done
yihtserns opened this issue Jul 21, 2024 · 3 comments
Closed
1 task done
Labels
2.18 Issues planned at 2.18 or later Record Issue related to JDK17 java.lang.Record support
Milestone

Comments

@yihtserns
Copy link
Contributor

yihtserns commented Jul 21, 2024

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:

@yihtserns yihtserns added the to-evaluate Issue that has been received but not yet evaluated label Jul 21, 2024
@cowtowncoder cowtowncoder added Record Issue related to JDK17 java.lang.Record support 2.18 Issues planned at 2.18 or later and removed to-evaluate Issue that has been received but not yet evaluated labels Jul 21, 2024
@yihtserns yihtserns changed the title @JsonProperty.access=READ_ONLY on Record property ignored for deserialization, if @JsonProperty.value is the same as the property name @JsonIgnore & @JsonProperty.access=READ_ONLY on Record property ignored for deserialization, if @JsonProperty.value is the same as the property name Jul 22, 2024
iifawzi added a commit to iifawzi/jackson-databind that referenced this issue Apr 6, 2025
cowtowncoder pushed a commit that referenced this issue Apr 6, 2025
…d-access when json renamed to same property name (#5073)
@cowtowncoder
Copy link
Member

Parts of this issue fixed via #5073 but some issue(s) still remain so left open.

@yihtserns
Copy link
Contributor Author

...specifically, this scenario is fixed:

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);

@cowtowncoder
Copy link
Member

Ok. And the remaining case is bit odd too; use of both @JsonIgnore and @JsonProperty (although I guess it could be used to match name-to-ignore).

I think I'll add a case with POJO to show how it should work.

cowtowncoder added a commit that referenced this issue Apr 7, 2025
@cowtowncoder cowtowncoder changed the title @JsonIgnore & @JsonProperty.access=READ_ONLY on Record property ignored for deserialization, if @JsonProperty.value is the same as the property name @JsonIgnore and @JsonProperty.access=READ_ONLY on Record property ignored for deserialization Apr 11, 2025
@cowtowncoder cowtowncoder added this to the 2.18.4 milestone Apr 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.18 Issues planned at 2.18 or later Record Issue related to JDK17 java.lang.Record support
Projects
None yet
Development

No branches or pull requests

2 participants