-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix #5398: ignore + rename #5410
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
Merged
+59
−3
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/test/java/com/fasterxml/jackson/databind/introspect/JsonPropertyRename5398Test.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package com.fasterxml.jackson.databind.introspect; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import com.fasterxml.jackson.annotation.*; | ||
| import com.fasterxml.jackson.databind.*; | ||
| import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| // [databind#5398] @JsonProperty on getter with @JsonIgnore on setter | ||
| // causes deserialization to fail since 2.18.4 | ||
| public class JsonPropertyRename5398Test extends DatabindTestUtil | ||
| { | ||
| static class Test5398 { | ||
| private String prop = "someValue"; | ||
|
|
||
| @JsonProperty(value = "renamedProp") | ||
| public String getProp() { | ||
| return prop; | ||
| } | ||
|
|
||
| @JsonIgnore | ||
| public void setProp(String prop) { | ||
| this.prop = prop; | ||
| } | ||
| } | ||
|
|
||
| private final ObjectMapper MAPPER = newJsonMapper(); | ||
|
|
||
| @Test | ||
| public void testRenamedPropertyWithIgnoredSetter5398() throws Exception | ||
| { | ||
| Test5398 original = new Test5398(); | ||
| String json = MAPPER.writeValueAsString(original); | ||
|
|
||
| // Should serialize with renamed property | ||
| assertEquals("{\"renamedProp\":\"someValue\"}", json); | ||
|
|
||
| // Should be able to deserialize back (setter is ignored, so field remains default) | ||
| Test5398 result = MAPPER.readValue(json, Test5398.class); | ||
| assertNotNull(result); | ||
| // Since setter is ignored, the deserialized object should have the default value | ||
| assertEquals("someValue", result.getProp()); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cowtowncoder just a nitpick but here I think the test is too ambiguous, it would have been better to set original's
propto a different value than the default one, to validate that it's indeed the default value that is used and not the one coming fromjson.I can submit a PR if you want :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ha! I tried and actually the comment
Is incorrect, if I remove the default value from the class and set it manually just before calling
writeValueAsString, thenresult.getProp()has the value "someValue".I tested on 2.17 too and it was like that also.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And this behaviour is actually consistent with when we don't rename the prop (cf my examples in #5398 (comment)). I proposed an improvement of this test here: #5412