What happened
An add operation whose array index is greater than the array length succeeds and appends the value.
v, _ := hujson.Parse([]byte(`[1,2]`))
err := v.Patch([]byte(`[{"op":"add","path":"/5","value":3}]`))
fmt.Println(err, string(v.Pack()))
// <nil> [1,2,3]
What you expected
RFC 6902, section 4.1 states that the index of an add on an array must not be greater than the number of elements. The patch should fail with an error and leave the array unchanged. Indexes equal to the length and the - token continue to append.
Cause
patchAdd in patch.go accepts any errNotFound from find as an insertion point. For arrays, find reports the array length as the index for every missing index, so /5 is treated the same as /2 or /-.
What happened
An
addoperation whose array index is greater than the array length succeeds and appends the value.What you expected
RFC 6902, section 4.1 states that the index of an
addon an array must not be greater than the number of elements. The patch should fail with an error and leave the array unchanged. Indexes equal to the length and the-token continue to append.Cause
patchAddinpatch.goaccepts anyerrNotFoundfromfindas an insertion point. For arrays,findreports the array length as the index for every missing index, so/5is treated the same as/2or/-.