-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix #5238 @JsonIdentityInfo
w/ Records
#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
Changes from 4 commits
1a9ae3f
3ea8ccd
04fb870
1607ccf
6ded5d4
131dd35
ca4c941
e0aca59
342c269
7d06fc5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.fasterxml.jackson.databind.records; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.fasterxml.jackson.annotation.*; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
// [databind#5238] immutable classes with @JsonIdentityInfo can be deserialized; records cannot | ||
public class JsonIdentityOnRecord5238Test | ||
extends DatabindTestUtil | ||
{ | ||
// 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this test should make sure One way would be to make constructor change "id" value by prefix or suffix. I know the end result does not look different, but it would explain discrepancy. |
||
this.id = id; | ||
this.name = name; | ||
} | ||
} | ||
|
||
private final ObjectMapper MAPPER = newJsonMapper(); | ||
|
||
@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); | ||
|
||
// Then : Check deserialization result, values | ||
ExamplePojo result = MAPPER.readValue(json, ExamplePojo.class); | ||
assertEquals(input.allThings.size(), result.allThings.size()); | ||
assertEquals(input.selected.id, result.selected.id); | ||
assertEquals(input.selected.name, result.selected.name); | ||
} | ||
|
||
@Test | ||
void testIdentityWithRecord() throws Exception { | ||
// Given | ||
ThingRecord t1 = new ThingRecord(1, "a"); | ||
ThingRecord t2 = new ThingRecord(2, "b"); | ||
ExampleRecord input = new ExampleRecord(List.of(t1, t2), t2); | ||
|
||
// When | ||
String json = MAPPER.writeValueAsString(input); | ||
ExampleRecord result = MAPPER.readValue(json, ExampleRecord.class); | ||
|
||
// Then | ||
assertEquals(input.allThings.size(), result.allThings.size()); | ||
assertEquals(input.selected.id, result.selected.id); | ||
assertEquals(input.selected.name, result.selected.name); | ||
} | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.