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

When a TypeReference is provided, @JsonTypeInfo should be ignored. #2063

Closed
nucatus opened this issue Jun 12, 2018 · 6 comments
Closed

When a TypeReference is provided, @JsonTypeInfo should be ignored. #2063

nucatus opened this issue Jun 12, 2018 · 6 comments

Comments

@nucatus
Copy link

nucatus commented Jun 12, 2018

In case of a deserialization where a TypeReference is provided, the deserializer should favor the type reference information in the detriment of a possible JsonTypeInfo annotation.

Since the information related to the concrete types to be used for deserialization is provided in a declarative way, it makes much more sense to override whatever strategy a @JsonTypeInfo annotation enforces.

POC

@cowtowncoder
Copy link
Member

I think this is fundamentally conflicting with the main idea of @JsonTypeInfo, but I think 2.9.6 (just released) does allow behavior of omitting type information.
If type information exists, however, I think it should have precedence over whatever user gave.

@nucatus
Copy link
Author

nucatus commented Jun 12, 2018

My argumentation in this case would be that annotations are a generic way, and here generic is used with the sense of broad, general, to express a configuration.

Using a declarative approach in the code is more granular and usually, the granular configuration should have precedence over the general/broad one.

BTW, can you please point me to some documentation on how to suppress JsonTypeInfo? Thanks!

@cowtowncoder
Copy link
Member

Ideally you avoid @JsonTypeInfo generally by not adding annotation.

But if you need to mask existing annotation, this can be done by custom AnnotationIntrospector, and overriding relevant method(s) (I forget specific name but JacksonAnnotationInstospector).
Or, you can use mix-in annotation to add @JsonTypeInfo with property of use = Id.NONE, which effectively means "do not use", and exists mostly to allow such blocking.

As to behavior itself: @JsonTypeInfo declares a thing (values of a class, or value of specific property) to require polymorphic handling. This is a fundamental property, and all handlers are built accordingly. It is not something that can be handled dynamically with current design.

Having said all that, overriding of just the root value type might be doable.

@cowtowncoder
Copy link
Member

Hmmh. Quickly looking at the test case, I am not quite sure this can be supported after all.
Problem is that the type info is not for root value directly, but for generic type within (element type of List that is a property of the non-generic root value).

But... unless this is radically simplified version, do you think you actually need @JsonTypeInfo in there?
It does not seem to serve a purpose since you do pass full type information (like you said).
If so I could see you wanting to add mix-in annotation for Child to disable @JsonTypeInfo handling altogether. Or, if common enough theme, to create custom JacksonAnnotationIntrospector sub-class that masks all or some instances out. Both would achieve your goal as far as I can see.

@nucatus
Copy link
Author

nucatus commented Jun 12, 2018

@cowtowncoder , thanks for the prompt replies.

Yes, this is a over simplified example in which @JsonTypeInfo wouldn't be necessary at all. I used is just for the sake of the demonstration. However, in our environment it is required because those objects are serialized/deserialized in another context where type information is needed.

And yes, the MixIn approach was something I was looking at.

@cowtowncoder cowtowncoder changed the title When a TypeReference is provided, JsonTypeInfo should be ignored. When a TypeReference is provided, @JsonTypeInfo should be ignored. Dec 4, 2020
@cowtowncoder
Copy link
Member

I think #2968 covers this: marking that one as "most-wanted", closing this.

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