-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix #5238 #5239
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
Open
JooHyukKim
wants to merge
8
commits into
FasterXML:2.19
Choose a base branch
from
JooHyukKim:fix-5238-
base: 2.19
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fix #5238 #5239
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1a9ae3f
Reproduce #5238
JooHyukKim 3ea8ccd
Add comment
JooHyukKim 04fb870
Finzlie #5238
JooHyukKim 1607ccf
Merge branch '2.19' into fix-5238-
JooHyukKim 6ded5d4
Use ClassUtil record check
JooHyukKim 131dd35
Merge branch 'fix-5238-' of https://github.com/JooHyukKim/jackson-dat…
JooHyukKim ca4c941
Merge branch '2.19' into fix-5238-
cowtowncoder e0aca59
Modify check to use Creator instead
JooHyukKim 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
84 changes: 84 additions & 0 deletions
84
src/test-jdk17/java/com/fasterxml/jackson/databind/records/JsonIdentityOnRecord5328Test.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,84 @@ | ||
package com.fasterxml.jackson.databind.records; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonIdentityInfo; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.ObjectIdGenerators; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
// [databind#5328] immutable classes with @JsonIdentityInfo can be deserialized; records cannot | ||
public class JsonIdentityOnRecord5328Test | ||
{ | ||
|
||
private final ObjectMapper MAPPER = new ObjectMapper(); | ||
|
||
// ✅ Working test with POJO | ||
@Test | ||
void testIdentityWithPojo() throws Exception { | ||
ThingPojo t1 = new ThingPojo(1, "a"); | ||
ThingPojo t2 = new ThingPojo(2, "b"); | ||
|
||
ExamplePojo input = new ExamplePojo(List.of(t1, t2), t2); | ||
|
||
String json = MAPPER.writeValueAsString(input); | ||
ExamplePojo result = MAPPER.readValue(json, ExamplePojo.class); | ||
|
||
assertEquals(input.selected.id, result.selected.id); | ||
assertEquals(input.selected.name, result.selected.name); | ||
} | ||
|
||
// ❌ Failing test with record | ||
@Test | ||
void testIdentityWithRecord() throws Exception { | ||
ThingRecord t1 = new ThingRecord(1, "a"); | ||
ThingRecord t2 = new ThingRecord(2, "b"); | ||
|
||
ExampleRecord input = new ExampleRecord(List.of(t1, t2), t2); | ||
|
||
String json = MAPPER.writeValueAsString(input); | ||
|
||
Exception ex = assertThrows(Exception.class, () -> | ||
MAPPER.readValue(json, ExampleRecord.class)); | ||
|
||
System.out.println("Expected failure:"); | ||
ex.printStackTrace(); | ||
} | ||
|
||
// Record-based data | ||
record ExampleRecord(List<ThingRecord> allThings, ThingRecord selected) {} | ||
|
||
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") | ||
record ThingRecord(int id, String name) {} | ||
|
||
// POJO-based data | ||
static class ExamplePojo { | ||
public List<ThingPojo> allThings; | ||
public ThingPojo selected; | ||
|
||
@JsonCreator | ||
public ExamplePojo( | ||
@JsonProperty("allThings") List<ThingPojo> allThings, | ||
@JsonProperty("selected") ThingPojo selected) { | ||
this.allThings = allThings; | ||
this.selected = selected; | ||
} | ||
} | ||
|
||
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") | ||
static class ThingPojo { | ||
public final int id; | ||
public final String name; | ||
|
||
@JsonCreator | ||
public ThingPojo(@JsonProperty("id") int id, @JsonProperty("name") String name) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.