diff --git a/jsonpatch.go b/jsonpatch.go index 3e69894..7f84a07 100644 --- a/jsonpatch.go +++ b/jsonpatch.go @@ -58,8 +58,8 @@ func NewPatch(operation, path string, value interface{}) Operation { // // An error will be returned if any of the two documents are invalid. func CreatePatch(a, b []byte) ([]Operation, error) { - aI := map[string]interface{}{} - bI := map[string]interface{}{} + var aI interface{} + var bI interface{} err := json.Unmarshal(a, &aI) if err != nil { return nil, errBadJSONDoc @@ -68,7 +68,7 @@ func CreatePatch(a, b []byte) ([]Operation, error) { if err != nil { return nil, errBadJSONDoc } - return diff(aI, bI, "", []Operation{}) + return handleValues(aI, bI, "", []Operation{}) } // Returns true if the values matches (must be json types) diff --git a/jsonpatch_test.go b/jsonpatch_test.go index 18c58be..5cc9bd3 100644 --- a/jsonpatch_test.go +++ b/jsonpatch_test.go @@ -704,7 +704,6 @@ var ( }` ) - var ( oldNestedObj = `{ "apiVersion": "kubedb.com/v1alpha1", @@ -738,7 +737,6 @@ var ( }` ) - func TestCreatePatch(t *testing.T) { cases := []struct { name string @@ -779,7 +777,10 @@ func TestCreatePatch(t *testing.T) { {"Kubernetes:Annotations", oldDeployment, newDeployment}, // crd with nested object {"Nested Member Object", oldNestedObj, newNestedObj}, + {"Array at root", `[{"asdf":"qwerty"}]`, `[{"asdf":"bla"},{"asdf":"zzz"}]`}, + {"Empty array at root", `[]`, `[{"asdf":"bla"},{"asdf":"zzz"}]`}, } + for _, c := range cases { t.Run(c.name+"[src->dst]", func(t *testing.T) { check(t, c.src, c.dst)