diff --git a/jsonpatch.go b/jsonpatch.go index 7f84a07..e7cb7d6 100644 --- a/jsonpatch.go +++ b/jsonpatch.go @@ -326,7 +326,7 @@ func backtrace(s, t []interface{}, p string, i int, j int, matrix [][]int) []Ope return append([]Operation{op}, backtrace(s, t, p, i-1, j-1, matrix)...) } - p2, _ := handleValues(s[j-1], t[j-1], makePath(p, i-1), []Operation{}) + p2, _ := handleValues(s[i-1], t[j-1], makePath(p, i-1), []Operation{}) return append(p2, backtrace(s, t, p, i-1, j-1, matrix)...) } if i > 0 && j > 0 && matrix[i-1][j-1] == matrix[i][j] { diff --git a/jsonpatch_test.go b/jsonpatch_test.go index 5cc9bd3..78d511f 100644 --- a/jsonpatch_test.go +++ b/jsonpatch_test.go @@ -737,6 +737,70 @@ var ( }` ) +var ( + oldArray = `{ + "apiVersion": "kubedb.com/v1alpha1", + "kind": "Elasticsearch", + "metadata": { + "name": "quick-elasticsearch", + "namespace": "demo" + }, + "spec": { + "tolerations": [ + { + "key": "node.kubernetes.io/key1", + "operator": "Equal", + "value": "value1", + "effect": "NoSchedule" + }, + { + "key": "node.kubernetes.io/key2", + "operator": "Equal", + "value": "value2", + "effect": "NoSchedule" + }, + { + "key": "node.kubernetes.io/not-ready", + "operator": "Exists", + "effect": "NoExecute", + "tolerationSeconds": 300 + }, + { + "key": "node.kubernetes.io/unreachable", + "operator": "Exists", + "effect": "NoExecute", + "tolerationSeconds": 300 + } + ] + } +}` + + newArray = `{ + "apiVersion": "kubedb.com/v1alpha1", + "kind": "Elasticsearch", + "metadata": { + "name": "quick-elasticsearch", + "namespace": "demo" + }, + "spec": { + "tolerations": [ + { + "key": "node.kubernetes.io/key2", + "operator": "Equal", + "value": "value2", + "effect": "NoSchedule" + }, + { + "key": "node.kubernetes.io/key1", + "operator": "Equal", + "value": "value1", + "effect": "NoSchedule" + } + ] + } +}` +) + func TestCreatePatch(t *testing.T) { cases := []struct { name string @@ -777,6 +841,8 @@ func TestCreatePatch(t *testing.T) { {"Kubernetes:Annotations", oldDeployment, newDeployment}, // crd with nested object {"Nested Member Object", oldNestedObj, newNestedObj}, + // array with different order + {"Different Array", oldArray, newArray}, {"Array at root", `[{"asdf":"qwerty"}]`, `[{"asdf":"bla"},{"asdf":"zzz"}]`}, {"Empty array at root", `[]`, `[{"asdf":"bla"},{"asdf":"zzz"}]`}, }