Skip to content
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

@JsonIdentityReference not used when setup on class only #1607

Closed
vboulaye opened this issue Apr 17, 2017 · 3 comments
Closed

@JsonIdentityReference not used when setup on class only #1607

vboulaye opened this issue Apr 17, 2017 · 3 comments
Milestone

Comments

@vboulaye
Copy link

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);
    }
@cowtowncoder
Copy link
Member

Thank you for reporting this; it sounds like a bug from first reading.

One quick question: is this with a recent version like 2.8.8?

@vboulaye
Copy link
Author

Yes, this is on a recent version, I am testing with 2.9.0.pr2 and I reproduced it on master.

@cowtowncoder cowtowncoder removed the 2.8 label Apr 18, 2017
@cowtowncoder cowtowncoder added this to the 2.8.9 milestone Apr 18, 2017
@cowtowncoder
Copy link
Member

Yes, fixed. Not sure if this is the best fix either (handling of defaulting done in a bit odd way), but handles this case, does not break any other tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants