Skip to content

Can't create a patch for top level arrays #21

Description

@e-nikolov
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{}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions