diff --git a/v5/patch.go b/v5/patch.go index f24e9d5..ad04eef 100644 --- a/v5/patch.go +++ b/v5/patch.go @@ -766,9 +766,9 @@ func ensurePathExists(pd *container, path string, options *ApplyOptions) error { } } - // Check if the next part is a numeric index. + // Check if the next part is a numeric index or "-". // If yes, then create an array, otherwise, create an object. - if arrIndex, err = strconv.Atoi(parts[pi+1]); err == nil { + if arrIndex, err = strconv.Atoi(parts[pi+1]); err == nil || parts[pi+1] == "-" { if arrIndex < 0 { if !options.SupportNegativeIndices { diff --git a/v5/patch_test.go b/v5/patch_test.go index 30fa7d5..c3a30e5 100644 --- a/v5/patch_test.go +++ b/v5/patch_test.go @@ -215,6 +215,34 @@ var Cases = []Case{ false, false, }, + { + `{ "foo": [{"bar": [{"baz0": "123"}]}]}`, + `[ { "op": "add", "path": "/foo/0/bar/-", "value": {"baz1": "456"} } ]`, + `{ "foo": [{"bar": [{"baz0": "123"}, {"baz1": "456"}]}]}`, + true, + true, + }, + { + `{ "foo": [{"bar": [{"baz0": "123"}]}]}`, + `[ { "op": "add", "path": "/foo/1/bar/0", "value": {"baz1": "456"} } ]`, + `{ "foo": [{"bar": [{"baz0": "123"}]}, {"bar": [{"baz1": "456"}]}]}`, + true, + true, + }, + { + `{ "foo": [{"bar": [{"baz0": "123"}]}]}`, + `[ { "op": "add", "path": "/foo/1/bar/-1", "value": {"baz1": "456"} } ]`, + `{ "foo": [{"bar": [{"baz0": "123"}]}, {"bar": [{"baz1": "456"}]}]}`, + true, + true, + }, + { + `{ "foo": [{"bar": [{"baz0": "123"}]}]}`, + `[ { "op": "add", "path": "/foo/1/bar/-", "value": {"baz1": "456"} } ]`, + `{ "foo": [{"bar": [{"baz0": "123"}]}, {"bar": [{"baz1": "456"}]}]}`, + true, + true, + }, { `{ "foo": "bar", "qux": { "baz": 1, "bar": null } }`, `[ { "op": "remove", "path": "/qux/bar" } ]`,