Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function ensurePathExists should handle appending correctly #148

Merged
merged 1 commit into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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] == "-" {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we should check for - here as it's intuitive behavior to me at least.

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"} } ]`,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other added testcases worked previously - this one failed.

`{ "foo": [{"bar": [{"baz0": "123"}]}, {"bar": [{"baz1": "456"}]}]}`,
true,
true,
},
{
`{ "foo": "bar", "qux": { "baz": 1, "bar": null } }`,
`[ { "op": "remove", "path": "/qux/bar" } ]`,
Expand Down