@@ -159,11 +159,18 @@ public CreatorValue build() {
159
159
}
160
160
}
161
161
162
+ // for [databind#761]
163
+
162
164
@ JsonDeserialize (builder =ValueInterfaceBuilder .class )
163
165
interface ValueInterface {
164
166
int getX ();
165
167
}
166
168
169
+ @ JsonDeserialize (builder =ValueInterface2Builder .class )
170
+ interface ValueInterface2 {
171
+ int getX ();
172
+ }
173
+
167
174
static class ValueInterfaceImpl implements ValueInterface
168
175
{
169
176
final int _x ;
@@ -177,6 +184,21 @@ public int getX() {
177
184
return _x ;
178
185
}
179
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
+
180
202
static class ValueInterfaceBuilder
181
203
{
182
204
public int x ;
@@ -190,10 +212,27 @@ public ValueInterface build() {
190
212
return new ValueInterfaceImpl (x );
191
213
}
192
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]
193
232
@ JsonDeserialize (builder = ValueBuilderWrongBuildType .class )
194
233
static class ValueClassWrongBuildType {
195
-
196
234
}
235
+
197
236
static class ValueBuilderWrongBuildType
198
237
{
199
238
public int x ;
@@ -207,7 +246,8 @@ public ValueClassXY build() {
207
246
return null ;
208
247
}
209
248
}
210
- /*
249
+
250
+ /*
211
251
/**********************************************************
212
252
/* Unit tests
213
253
/**********************************************************
@@ -217,25 +257,25 @@ public ValueClassXY build() {
217
257
218
258
public void testSimple () throws Exception
219
259
{
220
- String json = "{\" x\" :1,\" y\" :2}" ;
221
- Object o = mapper .readValue (json , ValueClassXY .class );
222
- assertNotNull (o );
223
- assertSame (ValueClassXY .class , o .getClass ());
224
- ValueClassXY value = (ValueClassXY ) o ;
225
- // note: ctor adds one to both values
226
- assertEquals (value ._x , 2 );
227
- 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 );
228
268
}
229
269
230
270
public void testMultiAccess () throws Exception
231
271
{
232
- String json = "{\" c\" :3,\" a\" :2,\" b\" :-9}" ;
233
- ValueClassABC value = mapper .readValue (json , ValueClassABC .class );
234
- assertNotNull (value );
235
- // note: ctor adds one to both values
236
- assertEquals (value .a , 2 );
237
- assertEquals (value .b , -9 );
238
- 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 );
239
279
}
240
280
241
281
// test for Immutable builder, to ensure return value is used
@@ -264,13 +304,22 @@ public void testWithCreator() throws Exception
264
304
assertEquals (3 , value .c );
265
305
}
266
306
267
- public void testBuilderMethodReturnInterface () throws Exception
307
+ // for [databind#761]
308
+
309
+ public void testBuilderMethodReturnMoreGeneral () throws Exception
268
310
{
269
311
final String json = "{\" x\" :1}" ;
270
312
ValueInterface value = mapper .readValue (json , ValueInterface .class );
271
313
assertEquals (2 , value .getX ());
272
314
}
273
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
+
274
323
public void testBuilderMethodReturnInvalidType () throws Exception
275
324
{
276
325
final String json = "{\" x\" :1}" ;
0 commit comments