Skip to content

Commit c6ef574

Browse files
authored
add tests for polymorphic subtype deserialization (#5017)
1 parent 817fb5e commit c6ef574

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.fasterxml.jackson.databind.jsontype;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
6+
import com.fasterxml.jackson.databind.MapperFeature;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
9+
import com.fasterxml.jackson.databind.exc.InvalidTypeIdException;
10+
import com.fasterxml.jackson.databind.json.JsonMapper;
11+
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
12+
13+
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static org.junit.jupiter.api.Assertions.assertThrows;
15+
16+
// https://github.com/FasterXML/jackson-databind/issues/5016
17+
public class PolymorphicDeserSubtypeCheck5016Test extends DatabindTestUtil
18+
{
19+
static abstract class Animal {
20+
public String name = "animal";
21+
}
22+
23+
static abstract class Plant {
24+
public String name = "plant";
25+
}
26+
27+
static class Cat extends Animal {
28+
public String name = "cat";
29+
}
30+
31+
static class Dog extends Animal implements Runnable {
32+
public String name = "dog";
33+
34+
@Override
35+
public void run() {
36+
System.out.println("Dog is running");
37+
}
38+
}
39+
40+
static class Tree extends Plant {
41+
public String name = "tree";
42+
}
43+
44+
static class AnimalInfo {
45+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS,
46+
include = JsonTypeInfo.As.PROPERTY,
47+
property = "@class")
48+
public Animal thisType;
49+
}
50+
51+
static class PlantInfo {
52+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS,
53+
include = JsonTypeInfo.As.PROPERTY,
54+
property = "@class")
55+
public Plant thisType;
56+
}
57+
58+
static class RunnableInfo {
59+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS,
60+
include = JsonTypeInfo.As.PROPERTY,
61+
property = "@class")
62+
public Runnable thisType;
63+
}
64+
65+
@Test
66+
public void testWrongSubtype() throws Exception {
67+
ObjectMapper mapper = newJsonMapper();
68+
PlantInfo plantInfo = new PlantInfo();
69+
plantInfo.thisType = new Tree();
70+
String serialized = mapper.writeValueAsString(plantInfo);
71+
PlantInfo newInfo0 = mapper.readValue(serialized, PlantInfo.class);
72+
assertEquals(plantInfo.thisType.name, newInfo0.thisType.name);
73+
// AnimalInfo has same JSON structure but incompatible type for `thisType`
74+
InvalidTypeIdException e = assertThrows(InvalidTypeIdException.class, () ->
75+
mapper.readValue(serialized, AnimalInfo.class));
76+
verifyException(e, "Could not resolve type id ");
77+
verifyException(e, "Not a subtype");
78+
}
79+
80+
// java.lang.Runnable not acceptable as safe base type
81+
@Test
82+
public void testBlockingOfRunnable() throws Exception {
83+
ObjectMapper mapper = JsonMapper.builder()
84+
.enable(MapperFeature.BLOCK_UNSAFE_POLYMORPHIC_BASE_TYPES)
85+
.build();
86+
AnimalInfo animalInfo = new AnimalInfo();
87+
animalInfo.thisType = new Dog();
88+
String serialized = mapper.writeValueAsString(animalInfo);
89+
AnimalInfo newInfo0 = mapper.readValue(serialized, AnimalInfo.class);
90+
assertEquals(animalInfo.thisType.name, newInfo0.thisType.name);
91+
try {
92+
mapper.readValue(serialized, RunnableInfo.class);
93+
} catch (InvalidDefinitionException e) {
94+
verifyException(e, "PolymorphicTypeValidator");
95+
verifyException(e, "denied resolution of all subtypes of base type `java.lang.Runnable`");
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)