jsonpatch icon indicating copy to clipboard operation
jsonpatch copied to clipboard

Can't create a patch for top level arrays

Open e-nikolov opened this issue 5 years ago • 0 comments

package test

import (
	"testing"

	"github.com/mattbaird/jsonpatch"

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

func TestJSONPatchCreate(t *testing.T) {
	cases := map[string]struct {
		a string
		b string
	}{
		"object": {
			`{"asdf": "qwerty"}`,
			`{"asdf": "zzz"}`,
		},
		"array": {
			`[{"asdf": "qwerty"}]`,
			`[{"asdf": "bla"}, {"asdf": "zzz"}]`,
		},
	}

	for name, tc := range cases {
		t.Run(name, func(t *testing.T) {
			_, err := jsonpatch.CreatePatch([]byte(tc.a), []byte(tc.b))
			require.NoError(t, err)
		})
	}
}

When I run the test above, the "object" case works, but the "array" case results in an error that states "Invalid JSON Document". Likely because jsonpatch.CreatePatch() tries to unmarshal into map[string]interface{} and doesn't try to unmarshal into []interface{}

e-nikolov avatar May 17 '19 11:05 e-nikolov