blob: 32897e659e5e8d10d2527bacebacc1e13e7703f8 [file] [log] [blame]
Tatu Saloranta10f2ce32018-04-18 21:53:35 -07001package com.fasterxml.jackson.databind.mixins;
2
3import java.io.IOException;
4
5import com.fasterxml.jackson.annotation.*;
6
7import com.fasterxml.jackson.databind.*;
8
9public class MapperMixinsCopy1998Test extends BaseMapTest
10{
11 final static String FULLMODEL="{\"format\":\"1.0\",\"child\":{\"type\":\"CHILD_B\",\"name\":\"testB\"},\"notVisible\":\"should not be present\"}";
12 final static String EXPECTED="{\"format\":\"1.0\",\"child\":{\"name\":\"testB\"}}";
13
14 static class MyModelView { }
15
16 interface MixinConfig {
17 interface MyModelRoot {
18 @JsonView(MyModelView.class)
19 public String getFormat();
20
21 @JsonView(MyModelView.class)
22 public MyModelChildBase getChild();
23 }
24
25 @JsonTypeInfo(use = JsonTypeInfo.Id.NONE, include = JsonTypeInfo.As.EXISTING_PROPERTY)
26 interface MyModelChildBase {
27 @JsonView(MyModelView.class)
28 public String getName();
29 }
30
31 }
32
33 @JsonPropertyOrder({ "format", "child" })
34 static class MyModelRoot {
35 @JsonProperty
36 private String format = "1.0";
37
38 public String getFormat() {
39 return format;
40 }
41 @JsonProperty
42 private MyModelChildBase child;
43
44 public MyModelChildBase getChild() {
45 return child;
46 }
47
48 public void setChild(MyModelChildBase child) {
49 this.child = child;
50 }
51
52 @JsonProperty
53 private String notVisible = "should not be present";
54
55 public String getNotVisible() {
56 return notVisible;
57 }
58 }
59
60 @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
61 @JsonSubTypes({
62 @JsonSubTypes.Type(value = MyChildA.class, name = "CHILD_A"),
63 @JsonSubTypes.Type(value = MyChildB.class, name = "CHILD_B")
64 })
65 abstract static class MyModelChildBase {
66 @JsonProperty
67 private String name;
68
69 public String getName() {
70 return name;
71 }
72
73 @JsonIgnore
74 public void setName(String name) {
75 this.name = name;
76 }
77 }
78
79 static class MyChildA extends MyModelChildBase {
80 public MyChildA(String name) {
81 setName(name);
82 }
83 }
84
85 static class MyChildB extends MyModelChildBase {
86 public MyChildB(String name) {
87 setName(name);
88 }
89 }
90
91 public void testB_KO() throws Exception
92 {
93 final ObjectMapper DEFAULT = defaultMapper();
94 MyModelRoot myModelInstance = new MyModelRoot();
95 myModelInstance.setChild(new MyChildB("testB"));
96
97 ObjectMapper myObjectMapper = DEFAULT.copy();
98
99 String postResult = getString(myModelInstance, myObjectMapper);
100 assertEquals(FULLMODEL, postResult);
101// System.out.println("postResult: "+postResult);
102
103 myObjectMapper = DEFAULT.copy();
104// myObjectMapper = defaultMapper();
105 myObjectMapper.addMixIn(MyModelRoot.class, MixinConfig.MyModelRoot.class)
106 .addMixIn(MyModelChildBase.class, MixinConfig.MyModelChildBase.class)
107 .disable(MapperFeature.DEFAULT_VIEW_INCLUSION)
108 .setConfig(myObjectMapper.getSerializationConfig().withView(MyModelView.class));
109
110 String result = getString(myModelInstance, myObjectMapper);
Tatu Saloranta10f2ce32018-04-18 21:53:35 -0700111 assertEquals(EXPECTED, result);
112
113 }
114
115 private String getString(MyModelRoot myModelInstance, ObjectMapper myObjectMapper) throws IOException {
116 return myObjectMapper.writerFor(MyModelRoot.class).writeValueAsString(myModelInstance);
117 }
118
119 private ObjectMapper defaultMapper()
120 {
121 return new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
122 .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
123 .configure(MapperFeature.ALLOW_COERCION_OF_SCALARS, false)
124 .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true)
125 ;
126 }
127}