Starcounter-Jack / JSON-Patch

Lean and mean Javascript implementation of the JSON-Patch standard (RFC 6902). Update JSON documents using delta patches.
MIT License
1.79k stars 215 forks source link

compare generates wrong path for array add #240

Closed rzorzorzo closed 4 years ago

rzorzorzo commented 4 years ago

RFC6901:

exactly the single character "-", making the new referenced value the (nonexistent) member after the last array element.

RFC6902:

An element to add to an existing array - whereupon the supplied value is added to the array at the indicated location. Any elements at or above the specified index are shifted one position to the right. The specified index MUST NOT be greater than the number of elements in the array. If the "-" character is used to index the end of the array (see [RFC6901]), this has the effect of appending the value to the array.

the following json-patch example is not conform with above specs:

var document = { contactDetails: { phoneNumbers: [] } }; var document2 == { contactDetails: { phoneNumbers: [{number:"555-123"}] } }; var patch = jsonpath.compare(document, document2) // patch = [ { op: "add", path: "/contactDetails/phoneNumbers/0", value: { number: "555-123" } } ];

according to the specs the patch should be:

{ op: "add", path: "/contactDetails/phoneNumbers/-", value: { number: "555-123" } }

this results in an error on the server when applying the patch (in java)

rzorzorzo commented 4 years ago

my bad. The specified index MUST NOT be greater than the number of elements in the array in this case 0 is the size of the array.