Skip to content

Commit

Permalink
Function ensurePathExists should handle appending correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelMue committed Aug 31, 2021
1 parent 3c3c85e commit 6b362cc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions v5/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 28 additions & 0 deletions v5/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" } ]`,
Expand Down

0 comments on commit 6b362cc

Please sign in to comment.