-
-
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
activateDefaultTyping(PolymorphicTypeValidator, ...)
applies to Default Typing use case, but not to @JsonTypeInfo
#2524
Comments
This was conscious decisions based on there being 2 different ways to enable Default Typing. I do think that documentation improvements would make sense, to outline that validator is only used for Default Typing, and not for use via That said, I am definitely open to ideas for further reducing likelihood on unsafe usage. Also: this is something that is probably better discussed on |
activateDefaultTyping(PolymorphicTypeValidator, ...)
applies to Default Typing use case, but not to @JsonTypeInfo
I think this is an interesting issue. @cowtowncoder, I read from your comment that you would like enabling default typing and setting a validator to be orthogonal. One can certainly see it like that. At the same time, the method for activating default typing now requires a mandatory validator. How can we make these two things consistent? |
I struggled with the @JsonTypeInfo(use = Id.CLASS)
public class MyBaseType { .... } defining that only subtypes are legal; or, on property public class Menagerie {
@JsonTypeInfo(use = Id.CLASS)
public Animal pet;
} because in both cases types that are possible to deserialize must be subtypes of a type user controls. The unsafe cases (with base type of For 3.0, this can be solved by simply defaulting to restrictive |
Ok; so, I think addition of #2587 (in 2.11) should help here, to enforce use of safe PTV for I guess that while I could be convinced to make
So I guess I can see that this is a bit confusing, but I am not sure how it could be made less so. |
Will close this issue due to additions in 2.x: I am still open to improvements of course, most specifically ideas for bigger changes in 3.0. But if and when so, please file a new issue. |
There are two ways to enable polymorphic type handling:
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, ...)
annotationactivateDefaultTyping()
methods or deprecated unsafeenableDefaultTyping()
Jackson 2.10 now allows specifying a validator for the classes during deserialization. There are also two ways to set a validator:
setPolymorphicTypeValidator()
method onObjectMapper
or its builder.activateDefaultTyping()
methodsI noticed that specifying a validator in one of the
activateDefaultTyping()
methods doesn't necessary mean that the validator is going to be called always.In particular, if
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, ...)
annotation is used but the validator is specified only viaactivateDefaultTyping()
,then deserialization is going to work but the validator won't be called.
To turn the validation on in this case, the validator has to be additionally specified via
setPolymorphicTypeValidator()
method:If I understand the javadoc for
ObjectMapper
correctly@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, ...)
means "explicit polymorphic types"activateDefaultTyping()
means "enabling automatic inclusion of type information, needed for proper deserialization of polymorphic types"It makes me think, these two cases are considered relatively independent.
However, polymorphic type handling is enabled in both cases.
I am wondering if
activateDefaultTyping()
should also set the specifiedPolymorphicTypeValidator
.Otherwise, someone may get confused by calling only
activateDefaultTyping(PolymorphicTypeValidator, ...)
and thinking this is safe enough.The following patch addresses it:
The text was updated successfully, but these errors were encountered: