Skip to content

Commit 6e9193f

Browse files
committed
Minor tweaks post #3568
1 parent aaf270d commit 6e9193f

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

src/main/java/com/fasterxml/jackson/databind/node/ArrayNode.java

+7
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ protected ArrayNode _withArray(JsonPointer origPtr,
143143
protected ObjectNode _withObjectAddTailElement(JsonPointer tail, boolean preferIndex)
144144
{
145145
final int index = tail.getMatchingIndex();
146+
if (index < 0) {
147+
return null;
148+
}
149+
146150
tail = tail.tail();
147151

148152
// First: did we complete traversal? If so, easy, we got our result
@@ -166,6 +170,9 @@ protected ObjectNode _withObjectAddTailElement(JsonPointer tail, boolean preferI
166170
protected ArrayNode _withArrayAddTailElement(JsonPointer tail, boolean preferIndex)
167171
{
168172
final int index = tail.getMatchingIndex();
173+
if (index < 0) {
174+
return null;
175+
}
169176
tail = tail.tail();
170177

171178
// First: did we complete traversal? If so, easy, we got our result

src/test/java/com/fasterxml/jackson/databind/node/ObjectNodeTest.java

+32-7
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ public void testRetain()
299299
assertEquals("c", ob.get("c").textValue());
300300
}
301301

302-
public void testValidWith() throws Exception
302+
public void testValidWithObject() throws Exception
303303
{
304304
ObjectNode root = MAPPER.createObjectNode();
305305
assertEquals("{}", MAPPER.writeValueAsString(root));
@@ -317,27 +317,52 @@ public void testValidWithArray() throws Exception
317317
assertEquals("{\"arr\":[]}", MAPPER.writeValueAsString(root));
318318
}
319319

320-
public void testInvalidWith() throws Exception
320+
public void testInvalidWithObject() throws Exception
321321
{
322322
JsonNode root = MAPPER.createArrayNode();
323323
try { // should not work for non-ObjectNode nodes:
324-
root.with("prop");
324+
root.withObject("/prop");
325325
fail("Expected exception");
326326
} catch (UnsupportedOperationException e) {
327-
verifyException(e, "not of type `ObjectNode`");
327+
verifyException(e, "Cannot replace context node (of type");
328+
verifyException(e, "ArrayNode");
328329
}
329330
// also: should fail of we already have non-object property
330331
ObjectNode root2 = MAPPER.createObjectNode();
331332
root2.put("prop", 13);
332333
try { // should not work for non-ObjectNode nodes:
333-
root2.with("prop");
334+
root2.withObject("/prop");
334335
fail("Expected exception");
335336
} catch (UnsupportedOperationException e) {
336-
verifyException(e, "has value that is not");
337+
verifyException(e, "Cannot replace `JsonNode` of type ");
338+
verifyException(e, "IntNode");
337339
}
338340
}
339341

340342
public void testInvalidWithArray() throws Exception
343+
{
344+
JsonNode root = MAPPER.createArrayNode();
345+
try { // should not work for non-ObjectNode nodes:
346+
root.withArray("/prop");
347+
fail("Expected exception");
348+
} catch (UnsupportedOperationException e) {
349+
verifyException(e, "Cannot replace context node (of type");
350+
verifyException(e, "ArrayNode");
351+
}
352+
// also: should fail of we already have non-Array property
353+
ObjectNode root2 = MAPPER.createObjectNode();
354+
root2.put("prop", 13);
355+
try { // should not work for non-ObjectNode nodes:
356+
root2.withArray("/prop");
357+
fail("Expected exception");
358+
} catch (UnsupportedOperationException e) {
359+
verifyException(e, "Cannot replace `JsonNode` of type ");
360+
verifyException(e, "IntNode");
361+
}
362+
}
363+
364+
// Test for pre-2.14 behavior for "simple property"
365+
public void testInvalidWithArrayLegacy() throws Exception
341366
{
342367
JsonNode root = MAPPER.createArrayNode();
343368
try { // should not work for non-ObjectNode nodes:
@@ -356,7 +381,7 @@ public void testInvalidWithArray() throws Exception
356381
verifyException(e, "has value that is not");
357382
}
358383
}
359-
384+
360385
public void testSetAll() throws Exception
361386
{
362387
ObjectNode root = MAPPER.createObjectNode();

src/test/java/com/fasterxml/jackson/databind/node/WithPathTest.java

+24
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ public void testValidWithObjectSimpleExisting() throws Exception
2828
_testValidWithObjectSimpleExisting(false);
2929
}
3030

31+
public void testInvalidWithObjectTrivial() throws Exception
32+
{
33+
ArrayNode root = MAPPER.createArrayNode();
34+
try {
35+
root.withObject(JsonPointer.compile("/a"));
36+
fail("Should not pass");
37+
} catch (UnsupportedOperationException e) {
38+
verifyException(e, "Cannot replace context node");
39+
verifyException(e, "ArrayNode");
40+
}
41+
}
42+
3143
private void _testValidWithObjectSimpleExisting(boolean compile) throws Exception
3244
{
3345
final String DOC_STR = a2q("{'a':{'b':42,'c':{'x':1}}}");
@@ -214,6 +226,18 @@ public void testValidWithArraySimple() throws Exception {
214226
_testValidWithArraySimple(false);
215227
}
216228

229+
public void testInvalidWithArrayTrivial() throws Exception
230+
{
231+
ArrayNode root = MAPPER.createArrayNode();
232+
try {
233+
root.withArray(JsonPointer.compile("/a"));
234+
fail("Should not pass");
235+
} catch (UnsupportedOperationException e) {
236+
verifyException(e, "Cannot replace context node");
237+
verifyException(e, "ArrayNode");
238+
}
239+
}
240+
217241
private void _testValidWithArraySimple(boolean compile) throws Exception
218242
{
219243
final String DOC_STR = a2q(

0 commit comments

Comments
 (0)