3
3
import com .fasterxml .jackson .annotation .JsonCreator ;
4
4
import com .fasterxml .jackson .annotation .JsonProperty ;
5
5
import com .fasterxml .jackson .annotation .JsonSetter ;
6
+ import com .fasterxml .jackson .core .JsonProcessingException ;
6
7
import com .fasterxml .jackson .databind .*;
7
8
import com .fasterxml .jackson .databind .annotation .JsonDeserialize ;
8
9
import com .fasterxml .jackson .databind .annotation .JsonPOJOBuilder ;
@@ -157,8 +158,96 @@ public CreatorValue build() {
157
158
return new CreatorValue (a , b , c );
158
159
}
159
160
}
160
-
161
- /*
161
+
162
+ // for [databind#761]
163
+
164
+ @ JsonDeserialize (builder =ValueInterfaceBuilder .class )
165
+ interface ValueInterface {
166
+ int getX ();
167
+ }
168
+
169
+ @ JsonDeserialize (builder =ValueInterface2Builder .class )
170
+ interface ValueInterface2 {
171
+ int getX ();
172
+ }
173
+
174
+ static class ValueInterfaceImpl implements ValueInterface
175
+ {
176
+ final int _x ;
177
+
178
+ protected ValueInterfaceImpl (int x ) {
179
+ _x = x +1 ;
180
+ }
181
+
182
+ @ Override
183
+ public int getX () {
184
+ return _x ;
185
+ }
186
+ }
187
+
188
+ static class ValueInterface2Impl implements ValueInterface2
189
+ {
190
+ final int _x ;
191
+
192
+ protected ValueInterface2Impl (int x ) {
193
+ _x = x +1 ;
194
+ }
195
+
196
+ @ Override
197
+ public int getX () {
198
+ return _x ;
199
+ }
200
+ }
201
+
202
+ static class ValueInterfaceBuilder
203
+ {
204
+ public int x ;
205
+
206
+ public ValueInterfaceBuilder withX (int x0 ) {
207
+ this .x = x0 ;
208
+ return this ;
209
+ }
210
+
211
+ public ValueInterface build () {
212
+ return new ValueInterfaceImpl (x );
213
+ }
214
+ }
215
+
216
+ static class ValueInterface2Builder
217
+ {
218
+ public int x ;
219
+
220
+ public ValueInterface2Builder withX (int x0 ) {
221
+ this .x = x0 ;
222
+ return this ;
223
+ }
224
+
225
+ // should also be ok: more specific type
226
+ public ValueInterface2Impl build () {
227
+ return new ValueInterface2Impl (x );
228
+ }
229
+ }
230
+
231
+ // for [databind#761]
232
+ @ JsonDeserialize (builder = ValueBuilderWrongBuildType .class )
233
+ static class ValueClassWrongBuildType {
234
+ }
235
+
236
+ static class ValueBuilderWrongBuildType
237
+ {
238
+ public int x ;
239
+
240
+ public ValueBuilderWrongBuildType withX (int x0 ) {
241
+ this .x = x0 ;
242
+ return this ;
243
+ }
244
+
245
+ public ValueClassXY build () {
246
+ return null ;
247
+ }
248
+ }
249
+
250
+ /*
162
251
/**********************************************************
163
252
/* Unit tests
164
253
/**********************************************************
@@ -168,25 +257,25 @@ public CreatorValue build() {
168
257
169
258
public void testSimple () throws Exception
170
259
{
171
- String json = "{\" x\" :1,\" y\" :2}" ;
172
- Object o = mapper .readValue (json , ValueClassXY .class );
173
- assertNotNull (o );
174
- assertSame (ValueClassXY .class , o .getClass ());
175
- ValueClassXY value = (ValueClassXY ) o ;
176
- // note: ctor adds one to both values
177
- assertEquals (value ._x , 2 );
178
- assertEquals (value ._y , 3 );
260
+ String json = "{\" x\" :1,\" y\" :2}" ;
261
+ Object o = mapper .readValue (json , ValueClassXY .class );
262
+ assertNotNull (o );
263
+ assertSame (ValueClassXY .class , o .getClass ());
264
+ ValueClassXY value = (ValueClassXY ) o ;
265
+ // note: ctor adds one to both values
266
+ assertEquals (value ._x , 2 );
267
+ assertEquals (value ._y , 3 );
179
268
}
180
269
181
270
public void testMultiAccess () throws Exception
182
271
{
183
- String json = "{\" c\" :3,\" a\" :2,\" b\" :-9}" ;
184
- ValueClassABC value = mapper .readValue (json , ValueClassABC .class );
185
- assertNotNull (value );
186
- // note: ctor adds one to both values
187
- assertEquals (value .a , 2 );
188
- assertEquals (value .b , -9 );
189
- assertEquals (value .c , 3 );
272
+ String json = "{\" c\" :3,\" a\" :2,\" b\" :-9}" ;
273
+ ValueClassABC value = mapper .readValue (json , ValueClassABC .class );
274
+ assertNotNull (value );
275
+ // note: ctor adds one to both values
276
+ assertEquals (value .a , 2 );
277
+ assertEquals (value .b , -9 );
278
+ assertEquals (value .c , 3 );
190
279
}
191
280
192
281
// test for Immutable builder, to ensure return value is used
@@ -214,4 +303,34 @@ public void testWithCreator() throws Exception
214
303
assertEquals (2 , value .b );
215
304
assertEquals (3 , value .c );
216
305
}
306
+
307
+ // for [databind#761]
308
+
309
+ public void testBuilderMethodReturnMoreGeneral () throws Exception
310
+ {
311
+ final String json = "{\" x\" :1}" ;
312
+ ValueInterface value = mapper .readValue (json , ValueInterface .class );
313
+ assertEquals (2 , value .getX ());
314
+ }
315
+
316
+ public void testBuilderMethodReturnMoreSpecific () throws Exception
317
+ {
318
+ final String json = "{\" x\" :1}" ;
319
+ ValueInterface2 value = mapper .readValue (json , ValueInterface2 .class );
320
+ assertEquals (2 , value .getX ());
321
+ }
322
+
323
+ public void testBuilderMethodReturnInvalidType () throws Exception
324
+ {
325
+ final String json = "{\" x\" :1}" ;
326
+ try {
327
+ mapper .readValue (json , ValueClassWrongBuildType .class );
328
+ fail ("Missing expected JsonProcessingException exception" );
329
+ } catch (JsonProcessingException e ) {
330
+ assertTrue (
331
+ "Exception cause must be IllegalArgumentException" ,
332
+ e .getCause () instanceof IllegalArgumentException );
333
+ }
334
+ }
335
+
217
336
}
0 commit comments