Skip to content

Commit 3ed0767

Browse files
committed
Add failing test for #2811
1 parent efb15ea commit 3ed0767

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.fasterxml.jackson.failing;
2+
3+
import java.io.IOException;
4+
5+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
6+
7+
import com.fasterxml.jackson.databind.*;
8+
import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver;
9+
import com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase;
10+
11+
public class JsonTypeInfoCustomResolver2811Test extends BaseMapTest
12+
{
13+
interface Vehicle {
14+
}
15+
16+
static class Car implements Vehicle {
17+
public int wheels;
18+
public String color;
19+
}
20+
21+
static class Bicycle implements Vehicle {
22+
public int wheels;
23+
public String bicycleType;
24+
}
25+
26+
static class Person<T extends Vehicle> {
27+
public String name;
28+
public VehicleType vehicleType;
29+
30+
@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "vehicleType")
31+
@JsonTypeIdResolver(VehicleTypeResolver.class)
32+
public T vehicle;
33+
}
34+
35+
public enum VehicleType {
36+
CAR(Car.class),
37+
BICYCLE(Bicycle.class);
38+
39+
public final Class<? extends Vehicle> vehicleClass;
40+
41+
VehicleType(Class<? extends Vehicle> vehicleClass) {
42+
this.vehicleClass = vehicleClass;
43+
}
44+
}
45+
46+
static class VehicleTypeResolver extends TypeIdResolverBase {
47+
48+
JavaType superType;
49+
50+
@Override
51+
public void init(JavaType bt) {
52+
this.superType = bt;
53+
}
54+
55+
@Override
56+
public String idFromValue(Object value) {
57+
return null;
58+
}
59+
60+
@Override
61+
public String idFromValueAndType(Object value, Class<?> suggestedType) {
62+
return null;
63+
}
64+
65+
@Override
66+
public JavaType typeFromId(DatabindContext context, String id) throws IOException {
67+
try {
68+
Class<? extends Vehicle> vehicleClass = VehicleType.valueOf(id).vehicleClass;
69+
return context.constructSpecializedType(superType, vehicleClass);
70+
} catch (IllegalArgumentException e) {
71+
return null;
72+
}
73+
}
74+
75+
@Override
76+
public JsonTypeInfo.Id getMechanism() {
77+
return JsonTypeInfo.Id.NAME;
78+
}
79+
}
80+
81+
private final ObjectMapper MAPPER = jsonMapperBuilder()
82+
.disable(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY)
83+
.disable(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE)
84+
.build();
85+
86+
// [databind#2811]
87+
public void testTypeInfoWithCustomResolver2811NoTypeId() throws Exception
88+
{
89+
String json = "{ \"name\": \"kamil\", \"vehicle\": {\"wheels\": 4, \"color\": \"red\"}}";
90+
Person<?> person = MAPPER.readValue(json, Person.class);
91+
assertEquals("kamil", person.name);
92+
assertNull(person.vehicleType);
93+
assertNull(person.vehicle);
94+
}
95+
96+
// Passing case for comparison
97+
/*
98+
public void testTypeInfoWithCustomResolver2811WithTypeId() throws Exception
99+
{
100+
String json = "{\n" +
101+
" \"name\": \"kamil\",\n" +
102+
" \"vehicleType\": \"CAR\",\n" +
103+
" \"vehicle\": {\n" +
104+
" \"wheels\": 4,\n" +
105+
" \"color\": \"red\"\n" +
106+
" }\n" +
107+
"}"
108+
;
109+
Person<?> person = MAPPER.readValue(json, Person.class);
110+
assertEquals("kamil", person.name);
111+
assertEquals(VehicleType.CAR, person.vehicleType);
112+
assertNotNull(person.vehicle);
113+
}
114+
*/
115+
}

0 commit comments

Comments
 (0)