Skip to content

Commit a8a52d4

Browse files
committed
Add skeletal test for #4407
1 parent 451b000 commit a8a52d4

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.fasterxml.jackson.databind.jsontype;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import com.fasterxml.jackson.annotation.*;
6+
7+
import com.fasterxml.jackson.databind.*;
8+
import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver;
9+
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
import static org.junit.jupiter.api.Assertions.assertNotNull;
13+
14+
public class CustomTypeIdResolver4407Test extends DatabindTestUtil
15+
{
16+
// for [databind#4407]
17+
static class Wrapper4407Prop {
18+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
19+
include = JsonTypeInfo.As.PROPERTY,
20+
property = "type")
21+
@JsonTypeIdResolver(Resolver4407_typex.class)
22+
public Base4407 wrapped;
23+
24+
Wrapper4407Prop() { }
25+
public Wrapper4407Prop(String v) {
26+
wrapped = new Impl4407(v);
27+
}
28+
}
29+
30+
@JsonSubTypes({ @JsonSubTypes.Type(value = Impl4407.class) })
31+
static class Base4407 { }
32+
33+
static class Impl4407 extends Base4407 {
34+
public String value;
35+
36+
Impl4407() { }
37+
public Impl4407(String v) { value = v; }
38+
}
39+
40+
static class Resolver4407_typex extends Resolver4407Base {
41+
public Resolver4407_typex() { super("typeX"); }
42+
}
43+
44+
static class Resolver4407_null extends Resolver4407Base {
45+
public Resolver4407_null() { super(null); }
46+
}
47+
48+
static abstract class Resolver4407Base implements TypeIdResolver {
49+
private final String _typeId;
50+
51+
Resolver4407Base(String typeId) {
52+
_typeId = typeId;
53+
}
54+
55+
@Override
56+
public void init(JavaType baseType) { }
57+
58+
@Override
59+
public String idFromValue(Object value) {
60+
return _typeId;
61+
}
62+
63+
@Override
64+
public String idFromValueAndType(Object value, Class<?> suggestedType) {
65+
return idFromValue(value);
66+
}
67+
68+
@Override
69+
public String idFromBaseType() {
70+
return null;
71+
}
72+
73+
@Override
74+
public JavaType typeFromId(DatabindContext ctxt, String id) {
75+
if (id.equals(_typeId)) {
76+
return ctxt.constructType(Impl4407.class);
77+
}
78+
return null;
79+
}
80+
81+
@Override
82+
public String getDescForKnownTypeIds() {
83+
return null;
84+
}
85+
86+
@Override
87+
public JsonTypeInfo.Id getMechanism() {
88+
return JsonTypeInfo.Id.CUSTOM;
89+
}
90+
}
91+
92+
/*
93+
/**********************************************************
94+
/* Unit tests
95+
/**********************************************************
96+
*/
97+
98+
private final ObjectMapper MAPPER = newJsonMapper();
99+
100+
// [databind#4407]
101+
@Test
102+
public void testTypeIdProp4407() throws Exception
103+
{
104+
final String EXP = a2q("{'wrapped':{'type':'typeX','value':'xyz'}}");
105+
assertEquals(EXP,
106+
MAPPER.writeValueAsString(new Wrapper4407Prop("xyz")));
107+
assertNotNull(MAPPER.readValue(EXP, Wrapper4407Prop.class));
108+
}
109+
}

0 commit comments

Comments
 (0)