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

possibility to not require JsonTypeInfo annotation for sealed classes? #3596

Open
pjfanning opened this issue Sep 8, 2022 · 3 comments
Open
Labels
to-evaluate Issue that has been received but not yet evaluated

Comments

@pjfanning
Copy link
Member

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:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@pjfanning pjfanning added the to-evaluate Issue that has been received but not yet evaluated label Sep 8, 2022
@cowtowncoder
Copy link
Member

cowtowncoder commented Sep 9, 2022

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).
Just a simple MapperFeature or something.

@pjfanning
Copy link
Member Author

@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.

@cowtowncoder
Copy link
Member

Right. Now that I think about this, however, I realize there is the big question of what @JsonTypeInfo defaults are. Or rather how to allow configuration -- a default like shown above is fine but there probably should be a way to customize it. Unfortunately we do not have JsonTypeInfo.Value type (unlike for some of annotations), although I guess one could be added.
Specifying a class/interface with annotation could work I suppose.

Another thing to add to AnnotationIntrospector -- or, perhaps, this would be the main thing that exposes everything? -- would be a method to call to ask if given type is a "sealed type".
This is where we have some options; one would be "yes/no/don't know" result, after which introspector (or something) would need to figure out @JsonTypeInfo parameterization.
Another that it's this method that would actually return the parameterization.

Yet another is of course to instead bolt this in via existing handling of @JsonTypeInfo... but it seems cleaner to separate this out.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to-evaluate Issue that has been received but not yet evaluated
Projects
None yet
Development

No branches or pull requests

2 participants