@@ -198,10 +198,11 @@ private void _verifyObjectReplaceFail(JsonNode doc, JsonPointer ptr, OverwriteMo
198
198
199
199
/*
200
200
/**********************************************************************
201
- /* Test methods, withObjectProperty()
201
+ /* Test methods, withObjectProperty()/withObject(exprOrProperty)
202
202
/**********************************************************************
203
203
*/
204
204
205
+ // [databind#4095]
205
206
public void testWithObjectProperty () throws Exception
206
207
{
207
208
ObjectNode root = MAPPER .createObjectNode ();
@@ -226,7 +227,7 @@ public void testWithObjectProperty() throws Exception
226
227
ObjectNode match3 = root2 .withObjectProperty ("b" );
227
228
assertNotSame (match , match3 );
228
229
assertEquals ("{\" b\" :{}}" , root2 .toString ());
229
-
230
+
230
231
// and then failing case
231
232
JsonNode root3 = MAPPER .readTree ("{\" c\" : 123}" );
232
233
try {
@@ -237,6 +238,46 @@ public void testWithObjectProperty() throws Exception
237
238
}
238
239
}
239
240
241
+ // [databind#4096]
242
+ public void testWithObjectAdnExprOrProp () throws Exception
243
+ {
244
+ ObjectNode root = MAPPER .createObjectNode ();
245
+
246
+ // First: create new property value
247
+ ObjectNode match = root .withObject ("a" );
248
+ assertTrue (match .isObject ());
249
+ assertEquals (a2q ("{}" ), match .toString ());
250
+ match .put ("value" , 42 );
251
+ assertEquals (a2q ("{'a':{'value':42}}" ), root .toString ());
252
+
253
+ // and then with JsonPointer expr
254
+ match = root .withObject ("/a/b" );
255
+ assertTrue (match .isObject ());
256
+ assertEquals (a2q ("{}" ), match .toString ());
257
+ assertEquals (a2q ("{'a':{'value':42,'b':{}}}" ), root .toString ());
258
+
259
+ // Then existing prop:
260
+ assertEquals (a2q ("{'value':42,'b':{}}" ),
261
+ root .withObject ("a" ).toString ());
262
+ assertEquals (a2q ("{}" ),
263
+ root .withObject ("/a/b" ).toString ());
264
+
265
+ // and then failing case
266
+ JsonNode root3 = MAPPER .readTree ("{\" c\" : 123}" );
267
+ try {
268
+ root3 .withObject ("c" );
269
+ fail ("Should not pass" );
270
+ } catch (UnsupportedOperationException e ) {
271
+ verifyException (e , "Cannot replace `JsonNode` of type " );
272
+ }
273
+ try {
274
+ root3 .withObject ("/c" );
275
+ fail ("Should not pass" );
276
+ } catch (UnsupportedOperationException e ) {
277
+ verifyException (e , "Cannot replace `JsonNode` of type " );
278
+ }
279
+ }
280
+
240
281
/*
241
282
/**********************************************************************
242
283
/* Test methods, withArray()
@@ -359,10 +400,11 @@ public void testWithArray3882() throws Exception
359
400
360
401
/*
361
402
/**********************************************************************
362
- /* Test methods, withArrayProperty()
403
+ /* Test methods, withArrayProperty()/withArray(exprOrProperty)
363
404
/**********************************************************************
364
405
*/
365
406
407
+ // [databind#4095]
366
408
public void testWithArrayProperty () throws Exception
367
409
{
368
410
ObjectNode root = MAPPER .createObjectNode ();
@@ -396,4 +438,33 @@ public void testWithArrayProperty() throws Exception
396
438
verifyException (e , "Cannot replace `JsonNode` of type " );
397
439
}
398
440
}
441
+
442
+ // [databind#4096]
443
+ public void testWithArrayAndExprOrProp () throws Exception
444
+ {
445
+ ObjectNode root = MAPPER .createObjectNode ();
446
+
447
+ // First: create new property value
448
+ ArrayNode match = root .withArray ("a" );
449
+ assertTrue (match .isArray ());
450
+ assertEquals (a2q ("[]" ), match .toString ());
451
+ match .add (42 );
452
+ assertEquals (a2q ("{'a':[42]}" ), root .toString ());
453
+
454
+ match = root .withArray ("/b" );
455
+ assertEquals (a2q ("{'a':[42],'b':[]}" ), root .toString ());
456
+
457
+ // Second: match existing Object property
458
+ assertEquals (a2q ("[42]" ), root .withArray ("a" ).toString ());
459
+ assertEquals (a2q ("[42]" ), root .withArray ("/a" ).toString ());
460
+
461
+ // and then failing case
462
+ JsonNode root3 = MAPPER .readTree ("{\" c\" : 123}" );
463
+ try {
464
+ root3 .withArrayProperty ("c" );
465
+ fail ("Should not pass" );
466
+ } catch (UnsupportedOperationException e ) {
467
+ verifyException (e , "Cannot replace `JsonNode` of type " );
468
+ }
469
+ }
399
470
}
0 commit comments