-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
I am trying to setup @JsonIdentityInfo/@JsonIdentityReference in order to serialize all references to a given class as Object Id (and deserialize them later using a custom ObjectIdResolver to retrieve the proper referenced instance)
I use @JsonIdentityReference(alwaysAsId=true) in order to enforce exporting the object id in all cases.
It does not work as expected when I define the annotation only on the class (but it works fine when I set it directly on the property). I would rather not have to define it on every property as I will probably miss some...
From what I see in BeanSerializerBase, the alwaysAsId is reset when not ObjectIdInfo is found on the accessor:
ObjectIdInfo objectIdInfo = intr.findObjectIdInfo(accessor);
if (objectIdInfo == null) {
// no ObjectId override, but maybe ObjectIdRef?
if (oiw != null) {
objectIdInfo = intr.findObjectReferenceInfo(accessor,
new ObjectIdInfo(NAME_FOR_OBJECT_REF, null, null, null));
oiw = _objectIdWriter.withAlwaysAsId(objectIdInfo.getAlwaysAsId());
Shouldn't it be kept to the current value when no override is found ?
I tried to set it back in the default ObjectIdInfo created with NAME_FOR_OBJECT_REF but I am not sure if this is the right way to fix this.
Here is test I added in TestObjectIdSerialization for this case:
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="id")
@JsonIdentityReference(alwaysAsId=true)
static class ReallyAlwaysAsId
{
public int value;
public ReallyAlwaysAsId() { this(0); }
public ReallyAlwaysAsId(int v) {
value = v;
}
}
@JsonPropertyOrder(alphabetic=true)
static class ReallyAlwaysContainer
{
@JsonIdentityReference(alwaysAsId=true)
public AlwaysAsId a = new AlwaysAsId(13);
public ReallyAlwaysAsId b = new ReallyAlwaysAsId(13);
}
public void testReallyAlwaysAsId() throws Exception
{
String json = MAPPER.writeValueAsString(new ReallyAlwaysContainer());
assertEquals("{\"a\":1,\"b\":2}", json);
}