• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.deser;
2 
3 import com.fasterxml.jackson.annotation.JsonAnySetter;
4 import com.fasterxml.jackson.annotation.JsonIdentityInfo;
5 import com.fasterxml.jackson.annotation.JsonIncludeProperties;
6 import com.fasterxml.jackson.annotation.JsonProperty;
7 import com.fasterxml.jackson.annotation.ObjectIdGenerators;
8 import com.fasterxml.jackson.databind.BaseMapTest;
9 import com.fasterxml.jackson.databind.DeserializationFeature;
10 import com.fasterxml.jackson.databind.JsonMappingException;
11 import com.fasterxml.jackson.databind.ObjectMapper;
12 import com.fasterxml.jackson.databind.ObjectReader;
13 
14 import java.util.Arrays;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18 
19 /**
20  * This unit test suite that tests use of {@link com.fasterxml.jackson.annotation.JsonIncludeProperties}
21  * annotation with deserialization.
22  */
23 public class IncludeWithDeserTest
24         extends BaseMapTest
25 {
26     @JsonIncludeProperties({"y", "z"})
27     static class OnlyYAndZ
28     {
29         int _x = 0;
30         int _y = 0;
31         int _z = 0;
32 
setX(int value)33         public void setX(int value)
34         {
35             _x = value;
36         }
37 
setY(int value)38         public void setY(int value)
39         {
40             _y = value;
41         }
42 
setZ(int value)43         public void setZ(int value)
44         {
45             _z = value;
46         }
47 
48         @JsonProperty("y")
replacementForY(int value)49         void replacementForY(int value)
50         {
51             _y = value * 2;
52         }
53     }
54 
55     @JsonIncludeProperties({"y", "z"})
56     static class OnlyY
57     {
58         public int x;
59 
60         public int y = 1;
61     }
62 
63     static class OnlyYWrapperForOnlyYAndZ
64     {
65         @JsonIncludeProperties("y")
66         public OnlyYAndZ onlyY;
67     }
68 
69     // for [databind#1060]
70     static class IncludeForListValuesY
71     {
72         @JsonIncludeProperties({"y"})
73         //@JsonIgnoreProperties({"z"})
74         public List<OnlyYAndZ> onlyYs;
75 
IncludeForListValuesY()76         public IncludeForListValuesY()
77         {
78             onlyYs = Arrays.asList(new OnlyYAndZ());
79         }
80     }
81 
82     @SuppressWarnings("serial")
83     @JsonIncludeProperties({"@class", "a"})
84     static class MyMap extends HashMap<String, String>
85     {
86     }
87 
88     static class MapWrapper
89     {
90         @JsonIncludeProperties({"a"})
91         public final HashMap<String, Integer> value = new HashMap<String, Integer>();
92     }
93 
94     @JsonIncludeProperties({"foo", "bar"})
95     @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class)
96     static class AnySetterObjectId
97     {
98         protected Map<String, AnySetterObjectId> values = new HashMap<String, AnySetterObjectId>();
99 
100         @JsonAnySetter
anySet(String field, AnySetterObjectId value)101         public void anySet(String field, AnySetterObjectId value)
102         {
103             // Ensure that it is never called with null because of unresolved reference.
104             assertNotNull(value);
105             values.put(field, value);
106         }
107     }
108 
109     /*
110     /**********************************************************
111     /* Test methods
112     /**********************************************************
113      */
114 
115     private final ObjectMapper MAPPER = objectMapper();
116 
testSimpleInclude()117     public void testSimpleInclude() throws Exception
118     {
119         OnlyYAndZ result = MAPPER.readValue(
120                 aposToQuotes("{ 'x':1, '_x': 1, 'y':2, 'z':3 }"),
121                 OnlyYAndZ.class);
122         assertEquals(0, result._x);
123         assertEquals(4, result._y);
124         assertEquals(3, result._z);
125     }
126 
testIncludeIgnoredAndUnrecognizedField()127     public void testIncludeIgnoredAndUnrecognizedField() throws Exception
128     {
129         ObjectReader r = MAPPER.readerFor(OnlyY.class);
130 
131         // First, fine to get "y" only:
132         OnlyY result = r.readValue(aposToQuotes("{'x':3, 'y': 4}"));
133         assertEquals(0, result.x);
134         assertEquals(4, result.y);
135 
136         // but fail on ignored properties.
137         r = r.with(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES);
138         try {
139             r.readValue(aposToQuotes("{'x':3, 'y': 4, 'z': 5}"));
140             fail("Should fail");
141         } catch (JsonMappingException e) {
142             verifyException(e, "Ignored field");
143         }
144 
145         // or fail on unrecognized properties
146         try {
147             r.readValue(aposToQuotes("{'y': 3, 'z':2 }"));
148             fail("Should fail");
149         } catch (JsonMappingException e) {
150             verifyException(e, "Unrecognized field");
151         }
152 
153         // or success with the both settings disabled.
154         r = r.without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
155         r = r.without(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES);
156         r.readValue(aposToQuotes("{'y': 3, 'z':2 }"));
157         assertEquals(4, result.y);
158     }
159 
160 
testMergeInclude()161     public void testMergeInclude() throws Exception
162     {
163         OnlyYWrapperForOnlyYAndZ onlyY = MAPPER.readValue(
164                 aposToQuotes("{'onlyY': {'x': 2, 'y':3, 'z': 4}}"),
165                 OnlyYWrapperForOnlyYAndZ.class
166         );
167         assertEquals(0, onlyY.onlyY._x);
168         assertEquals(6, onlyY.onlyY._y);
169         assertEquals(0, onlyY.onlyY._z);
170     }
171 
testListInclude()172     public void testListInclude() throws Exception
173     {
174         IncludeForListValuesY result = MAPPER.readValue(
175                 aposToQuotes("{'onlyYs':[{ 'x':1, 'y' : 2, 'z': 3 }]}"),
176                 IncludeForListValuesY.class);
177         assertEquals(0, result.onlyYs.get(0)._x);
178         assertEquals(4, result.onlyYs.get(0)._y);
179         assertEquals(0, result.onlyYs.get(0)._z);
180     }
181 
testMapWrapper()182     public void testMapWrapper() throws Exception
183     {
184         MapWrapper result = MAPPER.readValue(aposToQuotes("{'value': {'a': 2, 'b': 3}}"), MapWrapper.class);
185         assertEquals(2, result.value.get("a").intValue());
186         assertFalse(result.value.containsKey("b"));
187     }
188 
testMyMap()189     public void testMyMap() throws Exception
190     {
191         MyMap result = MAPPER.readValue(aposToQuotes("{'a': 2, 'b': 3}"), MyMap.class);
192         assertEquals("2", result.get("a"));
193         assertFalse(result.containsKey("b"));
194     }
195 
testForwardReferenceAnySetterComboWithInclude()196     public void testForwardReferenceAnySetterComboWithInclude() throws Exception
197     {
198         String json = aposToQuotes("{'@id':1, 'foo':2, 'foo2':2, 'bar':{'@id':2, 'foo':1}}");
199         AnySetterObjectId value = MAPPER.readValue(json, AnySetterObjectId.class);
200         assertSame(value.values.get("bar"), value.values.get("foo"));
201         assertNull(value.values.get("foo2"));
202     }
203 }
204