Skip to content

Commit 9b99cfe

Browse files
committed
Improve unit testing for trees vs polymorphic
1 parent 397e457 commit 9b99cfe

File tree

3 files changed

+111
-48
lines changed

3 files changed

+111
-48
lines changed

src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public boolean useForType(JavaType t)
222222
TimeZone.getTimeZone("GMT"),
223223
Base64Variants.getDefaultVariant() // 2.1
224224
);
225-
225+
226226
/*
227227
/**********************************************************
228228
/* Configuration settings, shared
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.fasterxml.jackson.databind.node;
2+
3+
import java.io.IOException;
4+
5+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
6+
import com.fasterxml.jackson.core.*;
7+
import com.fasterxml.jackson.databind.*;
8+
9+
public class TestTreeWithType extends BaseMapTest
10+
{
11+
public static class Foo {
12+
public String bar;
13+
14+
public Foo() { }
15+
16+
public Foo(String bar) {
17+
this.bar = bar;
18+
}
19+
}
20+
21+
/*
22+
/**********************************************************
23+
/* Unit tests
24+
/**********************************************************
25+
*/
26+
27+
private final ObjectMapper MAPPER = new ObjectMapper();
28+
29+
public void testValueAsStringWithoutDefaultTyping() throws Exception {
30+
31+
Foo foo = new Foo("baz");
32+
String json = MAPPER.writeValueAsString(foo);
33+
34+
JsonNode jsonNode = MAPPER.readTree(json);
35+
assertEquals(jsonNode.get("bar").textValue(), foo.bar);
36+
}
37+
38+
public void testValueAsStringWithDefaultTyping() throws Exception {
39+
final ObjectMapper mapper = new ObjectMapper();
40+
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
41+
42+
Foo foo = new Foo("baz");
43+
String json = mapper.writeValueAsString(foo);
44+
45+
JsonNode jsonNode = mapper.readTree(json);
46+
assertEquals(jsonNode.get("bar").textValue(), foo.bar);
47+
}
48+
49+
public void testReadTreeWithDefaultTyping() throws Exception
50+
{
51+
final String CLASS = Foo.class.getName();
52+
53+
final ObjectMapper mapper = new ObjectMapper();
54+
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL,
55+
JsonTypeInfo.As.PROPERTY);
56+
String json = "{\"@class\":\""+CLASS+"\",\"bar\":\"baz\"}";
57+
JsonNode jsonNode = mapper.readTree(json);
58+
assertEquals(jsonNode.get("bar").textValue(), "baz");
59+
}
60+
61+
public void testValueToTreeWithoutDefaultTyping() throws Exception {
62+
63+
Foo foo = new Foo("baz");
64+
JsonNode jsonNode = MAPPER.valueToTree(foo);
65+
assertEquals(jsonNode.get("bar").textValue(), foo.bar);
66+
}
67+
68+
public void testValueToTreeWithDefaultTyping() throws Exception {
69+
final ObjectMapper mapper = new ObjectMapper();
70+
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
71+
72+
Foo foo = new Foo("baz");
73+
JsonNode jsonNode = mapper.valueToTree(foo);
74+
assertEquals(jsonNode.get("bar").textValue(), foo.bar);
75+
}
76+
}
Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,57 @@
11
package com.fasterxml.jackson.failing;
22

3-
import com.fasterxml.jackson.annotation.JsonTypeInfo;
3+
import java.io.IOException;
4+
5+
import com.fasterxml.jackson.core.*;
46
import com.fasterxml.jackson.databind.*;
7+
import com.fasterxml.jackson.databind.module.SimpleModule;
58

69
public class TestTreeWithType extends BaseMapTest
710
{
8-
public static class Foo {
9-
public String bar;
1011

11-
public Foo() { }
12+
// [Issue#353]
13+
public class SavedCookie {
14+
public String name, value;
1215

13-
public Foo(String bar) {
14-
this.bar = bar;
16+
public SavedCookie() { }
17+
public SavedCookie(String n, String v) {
18+
name = n;
19+
value = v;
1520
}
1621
}
17-
22+
23+
public class SavedCookieDeserializer extends JsonDeserializer<SavedCookie> {
24+
@Override
25+
public SavedCookie deserialize(JsonParser jsonParser, DeserializationContext ctxt)
26+
throws IOException {
27+
ObjectCodec oc = jsonParser.getCodec();
28+
JsonNode node = oc.readTree(jsonParser);
29+
return new SavedCookie(node.path("name").textValue(),
30+
node.path("value").textValue());
31+
}
32+
}
1833
/*
1934
/**********************************************************
2035
/* Unit tests
2136
/**********************************************************
2237
*/
2338

24-
private final ObjectMapper mapper = new ObjectMapper();
25-
26-
public void testValueAsStringWithoutDefaultTyping() throws Exception {
27-
28-
Foo foo = new Foo("baz");
29-
String json = mapper.writeValueAsString(foo);
30-
31-
JsonNode jsonNode = mapper.readTree(json);
32-
assertEquals(jsonNode.get("bar").textValue(), foo.bar);
33-
}
34-
35-
public void testValueAsStringWithDefaultTyping() throws Exception {
36-
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
37-
38-
Foo foo = new Foo("baz");
39-
String json = mapper.writeValueAsString(foo);
40-
41-
JsonNode jsonNode = mapper.readTree(json);
42-
assertEquals(jsonNode.get("bar").textValue(), foo.bar);
43-
}
44-
45-
public void testReadTreeWithDefaultTyping() throws Exception
39+
public void testIssue353() throws Exception
4640
{
47-
final String CLASS = Foo.class.getName();
41+
ObjectMapper mapper = new ObjectMapper();
4842

49-
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL,
50-
JsonTypeInfo.As.PROPERTY);
51-
String json = "{\"@class\":\""+CLASS+"\",\"bar\":\"baz\"}";
52-
JsonNode jsonNode = mapper.readTree(json);
53-
assertEquals(jsonNode.get("bar").textValue(), "baz");
54-
}
55-
56-
public void testValueToTreeWithoutDefaultTyping() throws Exception {
43+
mapper.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.NON_FINAL, "@class");
5744

58-
Foo foo = new Foo("baz");
59-
JsonNode jsonNode = mapper.valueToTree(foo);
60-
assertEquals(jsonNode.get("bar").textValue(), foo.bar);
61-
}
45+
SimpleModule testModule = new SimpleModule("MyModule", new Version(1, 0, 0, null, "TEST", "TEST"));
46+
testModule.addDeserializer(SavedCookie.class, new SavedCookieDeserializer());
47+
mapper.registerModule(testModule);
6248

63-
public void testValueToTreeWithDefaultTyping() throws Exception {
64-
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
49+
SavedCookie savedCookie = new SavedCookie("key", "v");
50+
String json = mapper.writeValueAsString(savedCookie);
51+
52+
SavedCookie out = mapper.reader(SavedCookie.class).readValue(json);
6553

66-
Foo foo = new Foo("baz");
67-
JsonNode jsonNode = mapper.valueToTree(foo);
68-
assertEquals(jsonNode.get("bar").textValue(), foo.bar);
54+
assertEquals("key", out.name);
55+
assertEquals("v", out.value);
6956
}
7057
}

0 commit comments

Comments
 (0)