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