-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix deduction deserializer with DefaultTypeResolverBuilder #3505
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
cowtowncoder
merged 4 commits into
FasterXML:2.14
from
Bluexin:bugfix/PENDING_DeserFactoryCustomTypeResolverBuilder
Oct 1, 2022
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
70 changes: 70 additions & 0 deletions
70
...sterxml/jackson/databind/deser/TestDeserializerFactoryWithDefaultTypeResolverBuilder.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,70 @@ | ||
package com.fasterxml.jackson.databind.deser; | ||
|
||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import com.fasterxml.jackson.databind.BaseMapTest; | ||
import com.fasterxml.jackson.databind.DeserializationConfig; | ||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator; | ||
import com.fasterxml.jackson.databind.jsontype.NamedType; | ||
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
// Tests for [databind#pending] causing a NPE when setting a DefaultTypeResolverBuilder | ||
// and registering subtypes through ObjectMapper (no annotations) | ||
public class TestDeserializerFactoryWithDefaultTypeResolverBuilder extends BaseMapTest { | ||
|
||
private interface Parent { | ||
class ChildOne implements Parent { | ||
public String one; | ||
} | ||
|
||
class ChildTwo implements Parent { | ||
public String two; | ||
} | ||
} | ||
|
||
// This class is technically not needed for the test to fail without the fix | ||
// (AsDeductionTypeDeserializer will crash in #buildFingerprints), but was | ||
// added to have more assertions on the subtypes values | ||
private static final class AssertingTypeResolverBuilder extends ObjectMapper.DefaultTypeResolverBuilder { | ||
|
||
public AssertingTypeResolverBuilder() { | ||
super(ObjectMapper.DefaultTyping.NON_CONCRETE_AND_ARRAYS, | ||
BasicPolymorphicTypeValidator.builder().allowIfSubType(Parent.class).build()); | ||
} | ||
|
||
@Override | ||
public TypeDeserializer buildTypeDeserializer(DeserializationConfig config, JavaType baseType, Collection<NamedType> subtypes) { | ||
// this also gets called for String, not sure if that's expected with PTV | ||
// this behaviour is outside the scope of this test | ||
if (baseType.isAbstract()) { | ||
assertNotNull(subtypes); | ||
assertEquals(2, subtypes.size()); | ||
final List<NamedType> expected = new ArrayList<>(2); | ||
expected.add(new NamedType(Parent.ChildOne.class)); | ||
expected.add(new NamedType(Parent.ChildTwo.class)); | ||
assertTrue(subtypes.containsAll(expected)); | ||
} | ||
|
||
return super.buildTypeDeserializer(config, baseType, subtypes); | ||
} | ||
} | ||
|
||
public void testDeductionWithDefaultTypeResolverBuilder() { | ||
final ObjectMapper mapper = jsonMapperBuilder() | ||
.registerSubtypes(Parent.ChildOne.class, Parent.ChildTwo.class) | ||
.setDefaultTyping(new AssertingTypeResolverBuilder() | ||
.init(JsonTypeInfo.Id.DEDUCTION, null)) | ||
.build(); | ||
|
||
try { | ||
mapper.readValue("{ \"one\": \"Hello World\" }", Parent.class); | ||
} catch (Exception e) { | ||
fail("This call should not throw"); | ||
} | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.