-
-
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
Test illustrating handleUnknownTypeId problem #2222
Merged
Merged
Changes from all commits
Commits
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 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
23 changes: 23 additions & 0 deletions
23
src/test/java/com/fasterxml/jackson/databind/deser/GenericContent.java
This file contains 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,23 @@ | ||
package com.fasterxml.jackson.databind.deser; | ||
|
||
import java.util.Collection; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import com.fasterxml.jackson.annotation.JsonInclude.Include; | ||
|
||
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "_class") | ||
@JsonInclude(Include.NON_EMPTY) | ||
public class GenericContent { | ||
|
||
private Collection innerObjects; | ||
|
||
public Collection getInnerObjects() { | ||
return innerObjects; | ||
} | ||
|
||
public void setInnerObjects(Collection innerObjects) { | ||
this.innerObjects = innerObjects; | ||
} | ||
|
||
} |
78 changes: 78 additions & 0 deletions
78
src/test/java/com/fasterxml/jackson/databind/deser/HandleUnknowTypeIdTest.java
This file contains 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,78 @@ | ||
package com.fasterxml.jackson.databind.deser; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.fasterxml.jackson.core.JsonParseException; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.jsontype.TypeIdResolver; | ||
|
||
public class HandleUnknowTypeIdTest { | ||
|
||
public static class DummyContent { | ||
private String aField; | ||
|
||
public DummyContent() { | ||
super(); | ||
} | ||
|
||
public DummyContent(String aField) { | ||
super(); | ||
this.aField = aField; | ||
} | ||
|
||
public String getaField() { | ||
return aField; | ||
} | ||
|
||
public void setaField(String aField) { | ||
this.aField = aField; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "DummyContent [aField=" + aField + "]"; | ||
} | ||
} | ||
|
||
private ObjectMapper objectMapper; | ||
|
||
@Before | ||
public void setUp() { | ||
objectMapper = new ObjectMapper(); | ||
objectMapper.enableDefaultTyping(); | ||
} | ||
|
||
@Test | ||
public void testDeserializationWithDeserializationProblemHandler() throws JsonParseException, JsonMappingException, IOException { | ||
String dummyJson = IOUtils.toString(HandleUnknowTypeIdTest.class.getResourceAsStream("/com/fasterxml/jackson/databind/deser/DummyProcessableContent.json"), | ||
StandardCharsets.UTF_8); | ||
objectMapper.addHandler(new DeserializationProblemHandler() { | ||
@Override | ||
public JavaType handleUnknownTypeId(DeserializationContext ctxt, JavaType baseType, String subTypeId, TypeIdResolver idResolver, String failureMsg) throws IOException { | ||
System.out.println("Print out a warning here"); | ||
return ctxt.constructType(Void.class); | ||
} | ||
}); | ||
GenericContent processableContent = objectMapper.readValue(dummyJson, GenericContent.class); | ||
Assertions.assertThat(processableContent.getInnerObjects()).hasSize(2).usingFieldByFieldElementComparator().contains(new DummyContent("some value")); | ||
} | ||
|
||
@Test | ||
public void testDeserializationWithFAIL_ON_INVALID_SUBTYPE_false() throws JsonParseException, JsonMappingException, IOException { | ||
String dummyJson = IOUtils.toString(HandleUnknowTypeIdTest.class.getResourceAsStream("/com/fasterxml/jackson/databind/deser/DummyProcessableContent.json"), | ||
StandardCharsets.UTF_8); | ||
objectMapper.disable(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE); | ||
GenericContent processableContent = objectMapper.readValue(dummyJson, GenericContent.class); | ||
Assertions.assertThat(processableContent.getInnerObjects()).hasSize(2).usingFieldByFieldElementComparator().contains(new DummyContent("some value")); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/test/resources/com/fasterxml/jackson/databind/deser/DummyProcessableContent.json
This file contains 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,21 @@ | ||
{ | ||
"_class":"com.fasterxml.jackson.databind.deser.GenericContent", | ||
"innerObjects": | ||
[ | ||
"java.util.ArrayList", | ||
[ | ||
[ | ||
"com.fasterxml.jackson.databind.deser.HandleUnknowTypeIdTest$DummyContent", | ||
{ | ||
"aField":"some value" | ||
} | ||
], | ||
[ | ||
"com.fasterxml.jackson.databind.deser.HandleUnknowTypeIdTest$AnInventedClassBeingNotOnTheClasspath", | ||
{ | ||
"aField":"some value" | ||
} | ||
] | ||
] | ||
] | ||
} |
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.
Quick note just for future reference: I realize this is just for
test
scope, but I try to be careful for limiting number of dependencies added, so I'll change code not to need this.