Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

JSOG doesn't handle circular references correctly #26

Open
doraeimo opened this issue Jan 15, 2022 · 0 comments
Open

JSOG doesn't handle circular references correctly #26

doraeimo opened this issue Jan 15, 2022 · 0 comments

Comments

@doraeimo
Copy link

I'm serializing and deserializing a list including two objects, with circular references there.

//Using latest version for the dependencies
//jackson.version = 2.13.1
//jackson-jsog = 1.1.2

    @NoArgsConstructor
    @JsonIdentityInfo(generator = JSOGGenerator.class)
    public static class Outer {
        @Setter
        @Getter
        private Inner inner;
    }

    @NoArgsConstructor
    @JsonIdentityInfo(generator = JSOGGenerator.class)
    public static class Inner {

        @Getter
        private Outer outer;

        public Inner(Outer outer) {
            this.outer = outer;
            outer.setInner(this);
        }
    }

    public static void main(String[] args) throws Exception {
        // There are circular references between Outer and Inner
        Outer outer = new Outer();
        Inner inner = new Inner(outer);
        // Turn on type info
        PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder().allowIfSubType(Object.class).build();
        ObjectMapper mapper = new ObjectMapper()
                .activateDefaultTyping(ptv, ObjectMapper.DefaultTyping.EVERYTHING,
                        JsonTypeInfo.As.PROPERTY);

        List<Object> source = Lists.newArrayList(outer, inner);
        String json = mapper.writeValueAsString(source);
        System.out.println(json);
        //This is the json serialized by JSOGGenerator, which does make sense
        //See below
        List<Object> target = mapper.readerForListOf(Object.class).readValue(json);
        //However, I got an InvalidTypeIdException while deserializing
        //Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve subtype of [simple type, class com.voodoodyne.jackson.jsog.JSOGRef]: missing type id property '@class'

    }

This is the json serialized by JSOGGenerator, which does make sense.

[
    "java.util.ArrayList",
    [
        {
            "@class": "com.foo.bar.SomeTest$Outer",
            "@id": "1",
            "inner":
            {
                "@class": "com.foo.bar.SomeTest$Inner",
                "@id": "2",
                "outer":
                {
                    "@ref": "1"
                }
            }
        },
        {
            "@ref": "2"
        }
    ]
]

However, I got an InvalidTypeIdException while deserializing.
And I found the property "@id" is not handled correctly.

See ObjectIdValueProperty._valueDeserializer._baseType, which type is JSOGRef and it can not parse "1"(String) as object id value.

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

No branches or pull requests

1 participant