Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fasterxml.jackson.databind.deser.SettableAnyProperty;
import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.util.ClassUtil;

/**
* Simple container used for temporarily buffering a set of
Expand Down Expand Up @@ -335,7 +336,12 @@ public Object handleIdValue(final DeserializationContext ctxt, Object bean) thro
// also: may need to set a property value as well
SettableBeanProperty idProp = _objectIdReader.idProperty;
if (idProp != null) {
return idProp.setAndReturn(bean, _idValue);
// [databind#5328] Records do not have setters, skip...to set id value
if (ClassUtil.isRecordType(bean.getClass())) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have to think about this: ideally, setAndReturn() would not be called because id was passed via Constructor and no check was needed here.

Or maybe check could be done that if idProp is a CreatorProperty, no call would be made?

This is related to ensuring that we also do not try forcibly setting Field in regular POJO case, if id was already passed via Creator (see my note on test class).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's do that. Applied :)

return bean;
} else {
return idProp.setAndReturn(bean, _idValue);
}
}
} else {
// 07-Jun-2016, tatu: Trying to improve error messaging here...
Expand Down
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test should make sure id is only set via Constructor and NOT direct set on Field.
(JVM does allow forced settting of final Fields).

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

}
Loading