File tree 2 files changed +26
-3
lines changed
main/java/com/github/fge/jsonpatch
test/java/com/github/fge/jsonpatch
2 files changed +26
-3
lines changed Original file line number Diff line number Diff line change @@ -91,9 +91,13 @@ public JsonNode apply(final JsonNode node)
91
91
if (parentNode .isMissingNode ())
92
92
throw new JsonPatchException (BUNDLE .getMessage (
93
93
"jsonPatch.noSuchParent" ));
94
- return parentNode .isArray ()
95
- ? addToArray (path , node )
96
- : addToObject (path , node );
94
+ if (parentNode .isArray ())
95
+ return addToArray (path , node );
96
+ else if (parentNode .isObject ())
97
+ return addToObject (path , node );
98
+ else
99
+ throw new JsonPatchException (BUNDLE .getMessage (
100
+ "jsonPatch.noSuchPath" ));
97
101
}
98
102
99
103
private JsonNode addToArray (final JsonPointer path , final JsonNode node )
Original file line number Diff line number Diff line change 18
18
19
19
package com .github .fge .jsonpatch ;
20
20
21
+ import com .fasterxml .jackson .databind .node .ObjectNode ;
22
+ import com .fasterxml .jackson .databind .node .TextNode ;
23
+ import com .github .fge .jackson .JacksonUtils ;
24
+ import com .github .fge .jackson .jsonpointer .JsonPointer ;
25
+ import com .github .fge .jackson .jsonpointer .JsonPointerException ;
26
+ import org .testng .annotations .Test ;
27
+
21
28
import java .io .IOException ;
22
29
23
30
public final class AddOperationTest
@@ -28,4 +35,16 @@ public AddOperationTest()
28
35
{
29
36
super ("add" );
30
37
}
38
+
39
+ @ Test (expectedExceptions = JsonPatchException .class , expectedExceptionsMessageRegExp = "no such path in target JSON document" )
40
+ public void addingToANonContainerNodeThrowsException ()
41
+ throws JsonPatchException , JsonPointerException
42
+ {
43
+ final ObjectNode node = JacksonUtils .nodeFactory ().objectNode ();
44
+ final TextNode textNode = JacksonUtils .nodeFactory ().textNode ("bar" );
45
+ node .put ("foo" , textNode );
46
+ final JsonPointer p = new JsonPointer ("/foo/f" );
47
+ final JsonPatchOperation op = new AddOperation (p , JacksonUtils .nodeFactory ().nullNode ());
48
+ op .apply (node );
49
+ }
31
50
}
You can’t perform that action at this time.
0 commit comments