Skip to content

Commit 6b362cc

Browse files
committed
Function ensurePathExists should handle appending correctly
1 parent 3c3c85e commit 6b362cc

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

v5/patch.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -766,9 +766,9 @@ func ensurePathExists(pd *container, path string, options *ApplyOptions) error {
766766
}
767767
}
768768

769-
// Check if the next part is a numeric index.
769+
// Check if the next part is a numeric index or "-".
770770
// If yes, then create an array, otherwise, create an object.
771-
if arrIndex, err = strconv.Atoi(parts[pi+1]); err == nil {
771+
if arrIndex, err = strconv.Atoi(parts[pi+1]); err == nil || parts[pi+1] == "-" {
772772
if arrIndex < 0 {
773773

774774
if !options.SupportNegativeIndices {

v5/patch_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,34 @@ var Cases = []Case{
215215
false,
216216
false,
217217
},
218+
{
219+
`{ "foo": [{"bar": [{"baz0": "123"}]}]}`,
220+
`[ { "op": "add", "path": "/foo/0/bar/-", "value": {"baz1": "456"} } ]`,
221+
`{ "foo": [{"bar": [{"baz0": "123"}, {"baz1": "456"}]}]}`,
222+
true,
223+
true,
224+
},
225+
{
226+
`{ "foo": [{"bar": [{"baz0": "123"}]}]}`,
227+
`[ { "op": "add", "path": "/foo/1/bar/0", "value": {"baz1": "456"} } ]`,
228+
`{ "foo": [{"bar": [{"baz0": "123"}]}, {"bar": [{"baz1": "456"}]}]}`,
229+
true,
230+
true,
231+
},
232+
{
233+
`{ "foo": [{"bar": [{"baz0": "123"}]}]}`,
234+
`[ { "op": "add", "path": "/foo/1/bar/-1", "value": {"baz1": "456"} } ]`,
235+
`{ "foo": [{"bar": [{"baz0": "123"}]}, {"bar": [{"baz1": "456"}]}]}`,
236+
true,
237+
true,
238+
},
239+
{
240+
`{ "foo": [{"bar": [{"baz0": "123"}]}]}`,
241+
`[ { "op": "add", "path": "/foo/1/bar/-", "value": {"baz1": "456"} } ]`,
242+
`{ "foo": [{"bar": [{"baz0": "123"}]}, {"bar": [{"baz1": "456"}]}]}`,
243+
true,
244+
true,
245+
},
218246
{
219247
`{ "foo": "bar", "qux": { "baz": 1, "bar": null } }`,
220248
`[ { "op": "remove", "path": "/qux/bar" } ]`,

0 commit comments

Comments
 (0)