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

add DeserializationFeature.FAIL_ON_SUBTYPE_CLASS_NOT_REGISTERED #5027

Open
wants to merge 17 commits into
base: 2.19
Choose a base branch
from

Conversation

pjfanning
Copy link
Member

@pjfanning pjfanning commented Mar 16, 2025

The test is about polymorphic types. FooClass has 2 defined subclasses but I only register one (via a JsonSubTypes annotation).
So in theory, when I deserialize, I should be an exception when I try the 2nd unregisered class.

With Id.NAME, the subtypes are check at deserialization time. With Id.CLASS and Id.MINIMAL_CLASS, the subtypes are not checked.
DeserializationFeature.FAIL_ON_SUBTYPE_CLASS_NOT_REGISTERED defaults to false but when enabled, Id.CLASS and Id.MINIMAL_CLASS deserialization for subtypes will also fail if the classes in the inputs don't match the subtypes listed for the JsonTypeInfo annotation (via a JsonSubTypes annotation).

If you enable this feature, you should carefully test that your deserialization works. You will run into trouble if you have base types annotated with @JsonTypeInfo but don't have a @JsonSubTypes annotation that lists all the possible subtypes.

@pjfanning pjfanning changed the title add PolymorphicIdClassDeserTest that seems to not fail I would expect add PolymorphicIdClassDeserTest that I think should fail (but doesn't) Mar 16, 2025
@cowtowncoder
Copy link
Member

@pjfanning Subtypes need not be listed when using class name as type id since, well, class is found directly.

It should indeed fail when using "type name" option (which defaults to "simple" class name).

@pjfanning
Copy link
Member Author

@pjfanning Subtypes need not be listed when using class name as type id since, well, class is found directly.

It should indeed fail when using "type name" option (which defaults to "simple" class name).

I can confirm that the @JsonTypeInfo(use = JsonTypeInfo.Id.NAME) does enforce the subtypes list.

@cowtowncoder
Copy link
Member

Is this PR still needed?

FooClass res1 = MAPPER.readValue(foo1, FooClass.class);
assertTrue(res1 instanceof FooClassImpl);
// next bit should in theory fail because FooClassImpl2 is not listed as a subtype
FooClass res2 = MAPPER.readValue(foo2, FooClass.class);
Copy link
Member

@cowtowncoder cowtowncoder Mar 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. @JsonSubTypes is not used for any checking; it is only used for location subtypes when using "type name" (not class) as id, to find typeName-to-class mappings for subtypes on deserialization. This is not needed for class names.

Otherwise only Java language level subtype limitations are enforced (or checked).

EDIT: based on offline discussions, realize this is sort of RFE, asking to support such checks (optionally)

@pjfanning
Copy link
Member Author

@cowtowncoder I've added a DeserializationFeature.FAIL_ON_POLYMORPHIC_SUBTYPE_CLASS_NOT_EXPLICITLY_REGISTERED and the new check only happens if this is enabled.

I still need to work out how to support this for Id.MINIMAL_CLASS type too.

@pjfanning pjfanning changed the title add PolymorphicIdClassDeserTest that I think should fail (but doesn't) add DeserializationFeature.FAIL_ON_SUBTYPE_CLASS_NOT_REGISTERED Mar 28, 2025
@pjfanning
Copy link
Member Author

@cowtowncoder does this look like it's enough to merge?

@cowtowncoder
Copy link
Member

@pjfanning I hope to look into this soon (started 3.0.0-rc2 release)

@@ -26,6 +27,8 @@ public class ClassNameIdResolver

protected final PolymorphicTypeValidator _subTypeValidator;

private Set<String> _allowedSubtypes;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll change code a bit to make this final.

if (ctxt instanceof DeserializationContext) {
deserializationContext = (DeserializationContext) ctxt;
}
if (_allowedSubtypes != null && deserializationContext != null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without @JsonSubTypes, wouldn't _allowedSubtypes end up being null... ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess in the current impl, you can omit the JsonSubTypes annotation and then all subtypes are valid. Maybe I should change that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cowtowncoder I changed the code to enforce the check if enabled even when JsonSubTyoes are not defined

@@ -0,0 +1,61 @@
package com.fasterxml.jackson.databind.deser;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's different package for polymorphic deser... let me see.

@pjfanning pjfanning marked this pull request as draft March 29, 2025 12:30
@pjfanning pjfanning marked this pull request as ready for review March 29, 2025 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants