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

Test illustrating handleUnknownTypeId problem #2222

Merged
merged 1 commit into from
Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@
<version>1.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
Copy link
Member

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.

<artifactId>commons-io</artifactId>
<version>2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.11.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<!-- Alas, need to include snapshot reference since otherwise can not find
Expand Down
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;
}

}
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"));
}
}
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"
}
]
]
]
}