Skip to content

Commit f24de85

Browse files
committed
...
1 parent 3697046 commit f24de85

File tree

4 files changed

+218
-2
lines changed

4 files changed

+218
-2
lines changed

src/test/java/tools/jackson/databind/jsontype/NoTypeInfo1654CollectionTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ public void serialize(Value1654 value, JsonGenerator gen, SerializationContext c
9999
}
100100
}
101101

102+
/*
103+
/**********************************************************************
104+
/* Test methods
105+
/**********************************************************************
106+
*/
107+
102108
private final ObjectMapper MAPPER = newJsonMapper();
103109

104110
// [databind#1654]: no override, default polymorphic type id

src/test/java/tools/jackson/databind/jsontype/NoTypeInfo1654MapTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ public void serialize(Value1654 value, JsonGenerator gen, SerializationContext c
103103
}
104104
}
105105

106+
/*
107+
/**********************************************************************
108+
/* Test methods
109+
/**********************************************************************
110+
*/
111+
106112
private final ObjectMapper MAPPER = newJsonMapper();
107113

108114
// [databind#1654]: no override, default polymorphic type id for Map values
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
package tools.jackson.databind.jsontype;
2+
3+
import java.util.Optional;
4+
import java.util.concurrent.atomic.AtomicReference;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
9+
10+
import tools.jackson.core.JacksonException;
11+
import tools.jackson.core.JsonGenerator;
12+
import tools.jackson.core.JsonParser;
13+
14+
import tools.jackson.databind.*;
15+
import tools.jackson.databind.annotation.JsonDeserialize;
16+
import tools.jackson.databind.annotation.JsonSerialize;
17+
import tools.jackson.databind.testutil.DatabindTestUtil;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
21+
22+
/**
23+
* Test for Reference types (Optional, AtomicReference) with
24+
* {@code @JsonTypeInfo(use = Id.NONE)} override.
25+
*/
26+
class NoTypeInfo1654ReferenceTest extends DatabindTestUtil
27+
{
28+
// [databind#1654]
29+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
30+
static class Value1654 {
31+
public int x;
32+
33+
protected Value1654() { }
34+
35+
public Value1654(int x) {
36+
this.x = x;
37+
}
38+
}
39+
40+
static class Value1654TypedOptionalContainer {
41+
public Optional<Value1654> value;
42+
43+
protected Value1654TypedOptionalContainer() { }
44+
45+
public Value1654TypedOptionalContainer(Value1654 v) {
46+
value = Optional.ofNullable(v);
47+
}
48+
}
49+
50+
static class Value1654UntypedOptionalContainer {
51+
@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
52+
public Optional<Value1654> value;
53+
54+
protected Value1654UntypedOptionalContainer() { }
55+
56+
public Value1654UntypedOptionalContainer(Value1654 v) {
57+
value = Optional.ofNullable(v);
58+
}
59+
}
60+
61+
static class Value1654UsingCustomSerDeserUntypedOptionalContainer {
62+
@JsonDeserialize(contentUsing = Value1654Deserializer.class)
63+
@JsonSerialize(contentUsing = Value1654Serializer.class)
64+
@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
65+
public Optional<Value1654> value;
66+
67+
protected Value1654UsingCustomSerDeserUntypedOptionalContainer() { }
68+
69+
public Value1654UsingCustomSerDeserUntypedOptionalContainer(Value1654 v) {
70+
value = Optional.ofNullable(v);
71+
}
72+
}
73+
74+
static class Value1654TypedAtomicRefContainer {
75+
public AtomicReference<Value1654> value;
76+
77+
protected Value1654TypedAtomicRefContainer() { }
78+
79+
public Value1654TypedAtomicRefContainer(Value1654 v) {
80+
value = new AtomicReference<>(v);
81+
}
82+
}
83+
84+
static class Value1654UsingCustomSerDeserUntypedAtomicRefContainer {
85+
@JsonDeserialize(contentUsing = Value1654Deserializer.class)
86+
@JsonSerialize(contentUsing = Value1654Serializer.class)
87+
@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
88+
public AtomicReference<Value1654> value;
89+
90+
protected Value1654UsingCustomSerDeserUntypedAtomicRefContainer() { }
91+
92+
public Value1654UsingCustomSerDeserUntypedAtomicRefContainer(Value1654 v) {
93+
value = new AtomicReference<>(v);
94+
}
95+
}
96+
97+
static class Value1654Deserializer extends ValueDeserializer<Value1654> {
98+
@Override
99+
public Value1654 deserialize(JsonParser p, DeserializationContext ctxt) {
100+
JsonNode n = ctxt.readTree(p);
101+
if (!n.has("v")) {
102+
ctxt.reportInputMismatch(Value1654.class, "Bad JSON input (no 'v'): " + n);
103+
}
104+
return new Value1654(n.path("v").intValue());
105+
}
106+
}
107+
108+
static class Value1654Serializer extends ValueSerializer<Value1654> {
109+
@Override
110+
public void serialize(Value1654 value, JsonGenerator gen, SerializationContext ctxt)
111+
throws JacksonException {
112+
gen.writeStartObject(value);
113+
gen.writeNumberProperty("v", value.x);
114+
gen.writeEndObject();
115+
}
116+
}
117+
118+
/*
119+
/**********************************************************************
120+
/* Test methods
121+
/**********************************************************************
122+
*/
123+
124+
private final ObjectMapper MAPPER = newJsonMapper();
125+
126+
// [databind#1654]: no override, default polymorphic type id for Optional
127+
@Test
128+
void withoutNoTypeElementOverrideSerAndDeserOptional() throws Exception {
129+
Value1654TypedOptionalContainer cont = new Value1654TypedOptionalContainer(new Value1654(42));
130+
String json = MAPPER.writeValueAsString(cont);
131+
String typeId = Value1654.class.getName();
132+
typeId = "'@type':'" + typeId.substring(typeId.lastIndexOf('.') + 1) + "'";
133+
assertEquals(a2q("{'value':{"+typeId+",'x':42}}"), json);
134+
135+
Value1654TypedOptionalContainer result = MAPPER.readValue(json, Value1654TypedOptionalContainer.class);
136+
assertTrue(result.value.isPresent());
137+
assertEquals(42, result.value.get().x);
138+
}
139+
140+
// [databind#1654]: override, no polymorphic type id for Optional, serialization
141+
@Test
142+
void withNoTypeInfoDefaultSerOptional() throws Exception {
143+
Value1654UntypedOptionalContainer cont = new Value1654UntypedOptionalContainer(new Value1654(42));
144+
assertEquals(a2q("{'value':{'x':42}}"), MAPPER.writeValueAsString(cont));
145+
}
146+
147+
// [databind#1654]: override, no polymorphic type id for Optional, deserialization
148+
@Test
149+
void withNoTypeInfoDefaultDeserOptional() throws Exception {
150+
String noTypeJson = a2q("{'value':{'x':42}}");
151+
Value1654UntypedOptionalContainer result = MAPPER.readValue(noTypeJson,
152+
Value1654UntypedOptionalContainer.class);
153+
assertTrue(result.value.isPresent());
154+
assertEquals(42, result.value.get().x);
155+
}
156+
157+
// [databind#1654]: override, no polymorphic type id for Optional, custom serialization
158+
@Test
159+
void withNoTypeInfoOverrideSerOptional() throws Exception {
160+
Value1654UsingCustomSerDeserUntypedOptionalContainer cont =
161+
new Value1654UsingCustomSerDeserUntypedOptionalContainer(new Value1654(42));
162+
assertEquals(a2q("{'value':{'v':42}}"), MAPPER.writeValueAsString(cont));
163+
}
164+
165+
// [databind#1654]: override, no polymorphic type id for Optional, custom deserialization
166+
@Test
167+
void withNoTypeInfoOverrideDeserOptional() throws Exception {
168+
String noTypeJson = a2q("{'value':{'v':42}}");
169+
Value1654UsingCustomSerDeserUntypedOptionalContainer result = MAPPER.readValue(noTypeJson,
170+
Value1654UsingCustomSerDeserUntypedOptionalContainer.class);
171+
assertTrue(result.value.isPresent());
172+
assertEquals(42, result.value.get().x);
173+
}
174+
175+
// [databind#1654]: no override, default polymorphic type id for AtomicReference
176+
@Test
177+
void withoutNoTypeElementOverrideSerAndDeserAtomicRef() throws Exception {
178+
Value1654TypedAtomicRefContainer cont = new Value1654TypedAtomicRefContainer(new Value1654(42));
179+
String json = MAPPER.writeValueAsString(cont);
180+
String typeId = Value1654.class.getName();
181+
typeId = "'@type':'" + typeId.substring(typeId.lastIndexOf('.') + 1) + "'";
182+
assertEquals(a2q("{'value':{"+typeId+",'x':42}}"), json);
183+
184+
Value1654TypedAtomicRefContainer result = MAPPER.readValue(json, Value1654TypedAtomicRefContainer.class);
185+
assertEquals(42, result.value.get().x);
186+
}
187+
188+
// [databind#1654]: override, no polymorphic type id for AtomicReference, custom serialization
189+
@Test
190+
void withNoTypeInfoOverrideSerAtomicRef() throws Exception {
191+
Value1654UsingCustomSerDeserUntypedAtomicRefContainer cont =
192+
new Value1654UsingCustomSerDeserUntypedAtomicRefContainer(new Value1654(42));
193+
assertEquals(a2q("{'value':{'v':42}}"), MAPPER.writeValueAsString(cont));
194+
}
195+
196+
// [databind#1654]: override, no polymorphic type id for AtomicReference, custom deserialization
197+
@Test
198+
void withNoTypeInfoOverrideDeserAtomicRef() throws Exception {
199+
String noTypeJson = a2q("{'value':{'v':42}}");
200+
Value1654UsingCustomSerDeserUntypedAtomicRefContainer result = MAPPER.readValue(noTypeJson,
201+
Value1654UsingCustomSerDeserUntypedAtomicRefContainer.class);
202+
assertEquals(42, result.value.get().x);
203+
}
204+
}

src/test/java/tools/jackson/databind/jsontype/NoTypeInfoTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ final static class NoType implements NoTypeInterface {
2424
}
2525

2626
/*
27-
/**********************************************************
27+
/**********************************************************************
2828
/* Test methods
29-
/**********************************************************
29+
/**********************************************************************
3030
*/
3131

3232
@Test

0 commit comments

Comments
 (0)