Skip to content

Commit fe7c82d

Browse files
committed
Add a unit test reproducing an issue with custom id resolver, external property
1 parent 1fc74a7 commit fe7c82d

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

src/test/java/com/fasterxml/jackson/databind/jsontype/TestCustomTypeIdResolver.java

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,27 @@ public class TestCustomTypeIdResolver extends BaseMapTest
1717
{
1818
@JsonTypeInfo(use=Id.CUSTOM, include=As.WRAPPER_OBJECT)
1919
@JsonTypeIdResolver(CustomResolver.class)
20-
static class CustomBean {
20+
static abstract class CustomBean { }
21+
22+
static class CustomBeanImpl extends CustomBean {
2123
public int x;
2224

23-
public CustomBean() { }
24-
public CustomBean(int x) { this.x = x; }
25+
public CustomBeanImpl() { }
26+
public CustomBeanImpl(int x) { this.x = x; }
27+
}
28+
29+
static class CustomBeanWrapper {
30+
// @JsonTypeInfo(use=Id.NONE, include=As.EXTERNAL_PROPERTY, property="type")
31+
@JsonTypeInfo(use=Id.CUSTOM, include=As.PROPERTY, property="type")
32+
public CustomBean value;
2533
}
2634

2735
static class CustomResolver implements TypeIdResolver
2836
{
2937
static List<JavaType> initTypes;
3038

3139
public CustomResolver() { }
32-
40+
3341
@Override
3442
public Id getMechanism() {
3543
return Id.CUSTOM;
@@ -38,7 +46,7 @@ public Id getMechanism() {
3846
@Override
3947
public String idFromValue(Object value)
4048
{
41-
if (value.getClass() == CustomBean.class) {
49+
if (value instanceof CustomBean) {
4250
return "*";
4351
}
4452
return "unknown";
@@ -60,7 +68,7 @@ public void init(JavaType baseType) {
6068
public JavaType typeFromId(String id)
6169
{
6270
if ("*".equals(id)) {
63-
return TypeFactory.defaultInstance().constructType(CustomBean.class);
71+
return TypeFactory.defaultInstance().constructType(CustomBeanImpl.class);
6472
}
6573
return null;
6674
}
@@ -77,24 +85,40 @@ public String idFromBaseType() {
7785
/**********************************************************
7886
*/
7987

88+
private final ObjectMapper MAPPER = objectMapper();
89+
8090
// for [JACKSON-359]
8191
public void testCustomTypeIdResolver() throws Exception
8292
{
83-
ObjectMapper m = new ObjectMapper();
8493
List<JavaType> types = new ArrayList<JavaType>();
8594
CustomResolver.initTypes = types;
86-
String json = m.writeValueAsString(new CustomBean[] { new CustomBean(28) });
95+
String json = MAPPER.writeValueAsString(new CustomBean[] { new CustomBeanImpl(28) });
8796
assertEquals("[{\"*\":{\"x\":28}}]", json);
8897
assertEquals(1, types.size());
8998
assertEquals(CustomBean.class, types.get(0).getRawClass());
9099

91100
types = new ArrayList<JavaType>();
92101
CustomResolver.initTypes = types;
93-
CustomBean[] result = m.readValue(json, CustomBean[].class);
102+
CustomBean[] result = MAPPER.readValue(json, CustomBean[].class);
94103
assertNotNull(result);
95104
assertEquals(1, result.length);
96-
assertEquals(28, result[0].x);
105+
assertEquals(28, ((CustomBeanImpl) result[0]).x);
97106
assertEquals(1, types.size());
98107
assertEquals(CustomBean.class, types.get(0).getRawClass());
99108
}
109+
110+
public void testCustomWithExternal() throws Exception
111+
{
112+
CustomBeanWrapper w = new CustomBeanWrapper();
113+
w.value = new CustomBeanImpl(12);
114+
115+
String json = MAPPER.writeValueAsString(w);
116+
117+
System.out.println("JSON = "+json);
118+
119+
CustomBeanWrapper out = MAPPER.readValue(json, CustomBeanWrapper.class);
120+
assertNotNull(out);
121+
122+
assertEquals(12, ((CustomBeanImpl) out.value).x);
123+
}
100124
}

0 commit comments

Comments
 (0)