-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add new OptBoolean
valued property in @JsonTypeInfo
, handling, to allow per-polymorphic type loose Type Id handling
#3877
Comments
Here are some ideas I came up with. Let me know what you think! --personally my choice is solution#3. Summary (overall intuition)More specific configuration always overrides.
Solution 1: New @JsonTypeHandling annotation@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "@type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Dog.class, name = "dog"),
@JsonSubTypes.Type(value = Cat.class, name = "cat"),
})
public abstract class Animal {}
@JsonTypeHandling(looseHandling = true) // <---- NEW
public class Cat extends Animal {} Pros:
Cons:
Solution 2: Add new attribute to @JsonSubTypes.Type@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "@type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Dog.class, name = "dog"),
@JsonSubTypes.Type(value = Cat.class, name = "cat", looseHandling = true), // <---- NEW
})
public abstract class Animal {} Pros:
Cons:
Solution 3: Combine both solutions@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "@type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Dog.class, name = "dog"),
@JsonSubTypes.Type(value = Cat.class, name = "cat", looseHandling = true), // <---- NEW
})
public abstract class Animal {}
// ***overrides `looseHandling` attribute in @JsonSubTypes.Type
@JsonTypeHandling(looseHandling = true) // <---- NEW
public class Cow extends Animal {} Pros:
Cons:
Solution 4: Add attribute to existing
|
Hmmh. I can see benefits of doing of doing both. Although there is also the question on whether this is needed for individual subtypes, or just for base type? (that is, it always applies to all subtypes). My initial thinking was that it'd be for all subtypes of same base type, being simpler to implement and use. I guess it goes back to question of what exactly is the use case.... |
Thanks, @cowtowncoder 🙏🏽! I missed per base-type configuration using ‘@JsonTypeInfo` 🙈. Possible solutions are…
You are right it becomes the question of how specific a user wants to get in the configuration. But being a heavy user of |
Much of this also depends on how exactly annotation access is handled. Overriding global default is not super difficult, but trying to combine information from different annotations is, due to annotation flattening (it is not possible to know specific level at which given annotation is specified, to compare which comes from base/super class etc). |
Now adding attribute to |
@JooHyukKim Yes that sounds reasonable. |
@cowtowncoder great! 🙇🏼♂️ Once the default branch becomes 2.16, I will try to make a PR. Just in case anyone wonders how to work with new attribute in |
I replaced all occurances of |
@JooHyukKim Thank you for providing this! I merged it from 2.16 to master, but looks like test is failing. I wonder if you could help figure out why -- my guess is that some of earlier merges I made might have missed something. |
Thank you for letting me know 🙏🏼🙏🏼 No problem, will look into this today 👍🏻 |
Thank you for great guidance also 👍🏻👍🏻. Good to give back to the community. It was kinda thrilling to see #3891 get merged, because some PRs and issues prior to this one seem to sort of connect 😄. Such great experience. |
I made PR #3970 to fix the issue. It also has explanation. Hope it all helps, @cowtowncoder ! 🙏🏼 |
Thank you @JooHyukKim ! Will take a look. |
(note: offshoot of #3853)
It seems reasonable and useful to allow annotation(s) to specify loose/strict handling of Type Ids for polymorphic types.
The simplest location to do that would seem to be
@JsonTypeInfo
; this would only allow enabling/disabling for all (or none) subtypes but alternatives are probably more difficult to implement.The text was updated successfully, but these errors were encountered: