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

Builder Deserialization with Generics #17

Open
vjkoskela opened this issue Sep 1, 2016 · 0 comments
Open

Builder Deserialization with Generics #17

vjkoskela opened this issue Sep 1, 2016 · 0 comments

Comments

@vjkoskela
Copy link
Member

If the type is generic (e.g. Foo<T>) then its builder is also likely generic (e.g. Foo$Builder<T>). In such a case there is inevitably a setter on the builder that takes T (e.g. public void setBar(final T value)). When you deserialize json into such a type you do so with a type reference (e.g. new TypeReference<Foo<Bar>>(){}. Although the resulting pojo is constructed via its builder the provided type reference is for the resulting type and not its builder.

There is a bug in Jackson where the type binding information from the type reference is not carried over to the builder. Consequently, when deserializing content for setBar the type of the value is undetermined and is therefore deserialized by default as a TreeMap. The issue in Jackson is: FasterXML/jackson-databind#921

In such situations the recommended work around is to deserialize into the builder and then manually call build. This is easy if the generic type is at the top level; however, if it's buried in the type hierarchy you would need to use a custom serializer to achieve this same result at the desired level.

Tests were added for a generic and non-generic round-trip with builders through Jackson in #16. The generic test is disabled because it fails, but can be used to prove the resolution of this issue.

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

1 participant