-
Notifications
You must be signed in to change notification settings - Fork 173
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
Custom TypeResolverBuilder and registering subtypes in the ObjectMapper's configuration #568
Comments
I can't say for sure whether this would work with Kotlin module, but if you could do the same for plain old Specifically, if all the tests in And in fact I can transfer this issue to |
Sounds good to me ! |
Just to be clear, which branch should I target ? The change is small so it should be easy to swap branches, but you specifically mentioned 2.14 while the contributing doc state
|
For sake of completeness: yes, 2.14 is to be used in this particular case, just to minimize chance of regression for 2.13. |
Fixed in FasterXML/jackson-databind#3505 |
Hello,
I am using a custom
TypeResolverBuilder
to enable custom polymorphic deserialization for classes to which I can't add Jackson annotations. Here's a sample piece of (Kotlin) code, followed by some explanations :This custom
TypeResolverBuilder
delegates to instances ofObjectMapper.DefaultTypeResolverBuilder
to enable specific deserialization for each handled base type (it would be too easy if they were all the same). One of them usesJsonTypeInfo.Id.NAME
with an existing property (this one seems to work fine), and the other usesJsonTypeInfo.Id.DEDUCTION
.The latter fails because it expects subtypes to be non null. I would like to avoid having to hardcode them in the custom
TypeResolverBuilder
, and instead rely on the configuration'sSubtypeResolver
(akaJsonMapper.Builder::registerSubtypes
) for reusability.It could be that I'm going a completely wrong way about this. I also thought of using a custom
AnnotationIntrospector
instead, which might be the next thing I look into if this seems too bad.The reason I did not go for full custom (de)serializers is I hoped to get Afterburner/Blackbird to be able to kick in. It is not clear to me if it does for polymorphic deserialization, but if it doesn't I guess that would be a reasonable approach.
If my way seems like something that should work, I found a reason as to why it currently doesn't, with a potential fix (in Jackson Databind) :
https://github.com/FasterXML/jackson-databind/blob/f0af53d085eb2aa9f7f6199846cc526068e09977/src/main/java/com/fasterxml/jackson/databind/deser/BasicDeserializerFactory.java#L1782-L1795
Here I see the config's
SubtypeResolver
is only used when theTypeResolver
used is the one from theAnnotationIntrospector
.Is there a specific reason for this ?
From my testing it seemed safe to always query the subtypes from the config's
SubtypeResolver
, as it would result in an empty collection when none are found (which from my pov is nicer to work with than a nullable collection).Would there be anything obviously wrong with the following piece of code instead of the original ?
If this looks like a fix that could be made, I'll happily create a PR for this so I check no tests break etc.
Of course, if there is a proper easy way to implement what I need, I'll happily throw away these few hours of pulling hair trying to understand how all these internals are tied together :)
The text was updated successfully, but these errors were encountered: