-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add DeserializationFeature.FAIL_ON_SUBTYPE_CLASS_NOT_REGISTERED
#5027
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9f09361
Create PolymorphicIdClassDeserTest.java
pjfanning a460c2a
Merge branch '2.19' into subtype-test
cowtowncoder 17edca8
pass subtypes
pjfanning 9fa9678
Update ClassNameIdResolver.java
pjfanning 7c92fa8
add feature
pjfanning a1d812a
build issues
pjfanning c81688f
change feature name
pjfanning eb299c0
add test case
pjfanning 03fc82a
Minor tweaks
cowtowncoder f422ff6
Deprecate one method
cowtowncoder 8cb3fa1
Merge branch '2.19' into subtype-test
cowtowncoder 46243d4
Add "jackson-benchmarks" as downstream build dep
cowtowncoder 1f065e8
Merge branch 'subtype-test' of github.com:pjfanning/jackson-databind …
cowtowncoder d682fe8
Merge branch '2.19' into subtype-test
cowtowncoder ca3ddfd
fail if no subtype registered
pjfanning 14135d8
Update PolymorphicIdClassDeserTest.java
pjfanning 4fbd122
Merge branch '2.19' into subtype-test
pjfanning 9bcd018
Merge branch '2.19' into subtype-test
cowtowncoder a1a4054
Minor pre-merge tweaks
cowtowncoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/test/java/com/fasterxml/jackson/databind/deser/PolymorphicIdClassDeserTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.fasterxml.jackson.databind.deser; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's different package for polymorphic deser... let me see. |
||
|
||
import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.exc.InvalidTypeIdException; | ||
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class PolymorphicIdClassDeserTest extends DatabindTestUtil { | ||
|
||
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS) | ||
@JsonSubTypes({@JsonSubTypes.Type(value = FooClassImpl.class)}) | ||
cowtowncoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
static abstract class FooClass { } | ||
static class FooClassImpl extends FooClass { } | ||
static class FooClassImpl2 extends FooClass { } | ||
|
||
@JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS) | ||
@JsonSubTypes({@JsonSubTypes.Type(value = FooMinClassImpl.class)}) | ||
static abstract class FooMinClass { } | ||
static class FooMinClassImpl extends FooMinClass { } | ||
static class FooMinClassImpl2 extends FooMinClass { } | ||
|
||
/* | ||
/************************************************************ | ||
/* Unit tests, valid | ||
/************************************************************ | ||
*/ | ||
|
||
private final ObjectMapper MAPPER = jsonMapperBuilder() | ||
.enable(DeserializationFeature.FAIL_ON_SUBTYPE_CLASS_NOT_REGISTERED) | ||
.build(); | ||
|
||
@Test | ||
public void testDeserializationIdClass() throws Exception | ||
{ | ||
//trying to test if JsonSubTypes enforced | ||
final String foo1 = MAPPER.writeValueAsString(new FooClassImpl()); | ||
final String foo2 = MAPPER.writeValueAsString(new FooClassImpl2()); | ||
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 | ||
assertThrows(InvalidTypeIdException.class, () -> MAPPER.readValue(foo2, FooClass.class)); | ||
} | ||
|
||
@Test | ||
public void testDeserializationIdMinimalClass() throws Exception | ||
{ | ||
//trying to test if JsonSubTypes enforced | ||
final String foo1 = MAPPER.writeValueAsString(new FooMinClassImpl()); | ||
final String foo2 = MAPPER.writeValueAsString(new FooMinClassImpl2()); | ||
FooMinClass res1 = MAPPER.readValue(foo1, FooMinClass.class); | ||
assertTrue(res1 instanceof FooMinClassImpl); | ||
// next bit should in theory fail because FooMinClassImpl2 is not listed as a subtype | ||
assertThrows(InvalidTypeIdException.class, () -> MAPPER.readValue(foo2, FooMinClass.class)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 beingnull
... ?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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