Skip to content
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: 3 additions & 1 deletion jsonpatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,9 @@ func compareArray(av, bv []interface{}, p string) []Operation {
break
}
}
if !found {

newElementButEqualToAnExistingOne := found && i >= len(av)
if !found || newElementButEqualToAnExistingOne {
retval = append([]Operation{NewPatch("add", makePath(p, i), v)}, retval...)
}
}
Expand Down
25 changes: 25 additions & 0 deletions jsonpatch_subarray_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package jsonpatch

import (
"log"
"testing"

"github.com/stretchr/testify/assert"
)

var subArray1_current = `{"created":1739943104628936621,"updated":0,"index":"test","path":"test","data":{"subFields":[{"one":"one","two":"two","three":3}]}}`
var subArray1_target = `{"created":1739943104628936621,"updated":1739942662496282458,"index":"test","path":"test","data":{"subFields":[{"one":"one","two":"two","three":3},{"one":"one","two":"two","three":3}]}}`

var subArray2_current = `{"created":1739943104628936621,"updated":1739942662496282458,"index":"test","path":"test","data":{"subFields":[{"one":"one","two":"two","three":3},{"one":"one","two":"two","three":3}]}}`
var subArray2_target = `{"created":1739943104628936621,"updated":1739942662496282459,"index":"test","path":"test","data":{"subFields":[{"one":"one","two":"two","three":3},{"one":"one","two":"two","three":3},{"one":"one","two":"two","three":3}]}}`

func TestSubfieldArray(t *testing.T) {
patch, e := CreatePatch([]byte(subArray1_current), []byte(subArray1_target))
assert.NoError(t, e)
assert.Equal(t, len(patch), 2, "the patch should update 2 fields")

patch, e = CreatePatch([]byte(subArray2_current), []byte(subArray2_target))
assert.NoError(t, e)
log.Println("OP", patch)
assert.Equal(t, len(patch), 2, "the patch should update 2 fields")
}