jsonpatch icon indicating copy to clipboard operation
jsonpatch copied to clipboard

Empty patch when compare two arrays

Open tochti opened this issue 7 years ago • 3 comments

Hello,

first thanks for your effort creating this library.

I was wondering if the following behavior is intended?

Actual Behavior

If the following code is executed the result is an empty patch. The reason for that is that the 2nd item in o2 is the "same" item like the item in o1.

import (
	"fmt"

	"github.com/mattbaird/jsonpatch"
)

func main() {

	o1 := []byte(`{"Items":[{"K":1}]}`)
	o2 := []byte(`{"Items":[{"K":1},{"K":1}]}`)

	patch, err := jsonpatch.CreatePatch(o1, o2)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("Patch", patch)

}

Expected Behavior

I did expect to get the following patch

[{"op": "add", "path": "/Items/1", "value": {"K":1}}]

Specifications

Version: newest (go get github.com/mattbaird/jsonpatch)

tochti avatar Mar 14 '18 19:03 tochti

@tamalsaha Is this issue fixed in your fork?

mengqiy avatar Jan 07 '19 21:01 mengqiy

I believe so. You can add the test to https://github.com/appscode/jsonpatch/blob/master/jsonpatch_test.go and double check.

tamalsaha avatar Jan 07 '19 21:01 tamalsaha

@mengqiy , I believe this array test case is covered by https://github.com/appscode/jsonpatch/blob/master/jsonpatch_test.go#L758

tamalsaha avatar Jan 08 '19 04:01 tamalsaha