-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
possibility to not require JsonTypeInfo annotation for sealed classes? #3596
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
Comments
Yes, I agree. See #3549 for related work. I do think this probably needs to be somehow opt-in since there are probably some security concerns (I can't think of specifics but this tingles my spider sense wrt past experiences). |
@cowtowncoder a MapperFeature to enable a default JsonTypeInfo for sealed classes would be fine. Once enabled, any class that has subtypes would automatically support be deserialized as if it has the default JsonTypeInfo annotation on it. |
Right. Now that I think about this, however, I realize there is the big question of what Another thing to add to Yet another is of course to instead bolt this in via existing handling of Regardless, modules (Kotlin, Scala, Java-17?) would provide their AnnotationIntrospectors to detect sealed types they know of and handle. Does this sound good? /cc @sigpwned apologies for your sealed class PR falling through the cracks -- but I think it might be possible to join forces here. The concept of sealed class is indeed a common shared concern. |
I think #5025 implemented this? |
I failed to find any tests in #5025 that do not require the JsonTypeInfo annotation. The point of this issue is to allow users to omit the JsonTypeInfo annotation altogether. That is support polymorphism where no annotations are used. |
Ahhh. Ok yes. Teaches me not to actually read the discussion before commenting. So my earlier comment still stands: I would be against automatically assuming |
For my part, that seems smart, @cowtowncoder. There are way too many variables involved in serializing sealed types without a guiding
Baking in #5025 dramatically reduces the amount of annotation required to serialize a sealed type, so it's at least much less painful, even if it doesn't eliminate the boilerplate entirely. Only the top-level Regarding eliminating the annotation entirely, I suspect it would be possible to create a module that provides default behavior for sealed classes. But I think that's where that kind of functionality belongs -- in a module -- at least for now. Maybe it'll make sense to upstream it one day. Just my two cents, for whatever that's worth! And sorry I missed your mention earlier in the thread! At least we got there in the end. :) |
Agree on those points (name clashes etc). And while I am sympathetic to aim of reducing annotations, I also think defaults should have minimal chance of adding hard-to-find problems; and enabling of polymorphic handling where one is not requested seems like one. |
Let's start with something that is not enabled by default. But in my head, if someone has a sealed class or interface or abstract class as the type for a field, then I think they expect polymorphism support even if you may need to supply info at mapper config level as to how to include typing info in the serialised data and how to deserialize that info. |
While I can see polymorphic handling as commonly expected, I think there will also be usage for serialization-only cases where PM is not desired. But the configurability aspect is big too: maybe as-property style is dominant, but how about chosen type id property -- what would be the default? |
|
@pjfanning That sounds like a plan -- since
it should not be too hard to implement defaulting. |
One other thought: to allow per-class
this would not allow completely config-free set up, but would avoid use of annotations. |
Wrote #5070 failing-test so we have something to start with. We can also add config-override code along the way. @cowtowncoder Do you recall we had some work done for config-overrride for |
@JooHyukKim We can, although I am not sure this is amongst higher priority things for 3.0. But it is something that could be done. Note tho that my reference of config-overrides does not directly address doing #3596 itself. |
Scala, Kotlin and Java 17 have sealed classes. It is possible to write introspectors that find the subtypes.
It would be great if jackson-databind could infer a default JsonTypeInfo setting for sealed traits/interfaces/abstract-classes
A default like this, perhaps:
The text was updated successfully, but these errors were encountered: