Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text diff is incorrectly deserialized #31

Open
alexmt opened this issue Nov 30, 2018 · 0 comments
Open

Text diff is incorrectly deserialized #31

alexmt opened this issue Nov 30, 2018 · 0 comments

Comments

@alexmt
Copy link

alexmt commented Nov 30, 2018

The text diff is incorrectly deserialized if diff consists of text insert + text delete. Golang snippet below demonstrates the problem.

package diff

import (
	"encoding/json"
	"os"

	"github.com/yudai/gojsondiff"
	"github.com/yudai/gojsondiff/formatter"
	"golang.org/x/crypto/ssh/terminal"
)

func main() {
	left := make(map[string]interface{})
	right := make(map[string]interface{})
	_ = json.Unmarshal([]byte(`{ "data": { "int": 1, "string": "gcr.io/heptio-images/ks-guestbook-demo:0.1" } }`), &left)
	_ = json.Unmarshal([]byte(`{ "data": { "int": 2, "string": "gcr.io/heptio-images/ks-guestbook-demo:0.2" } }`), &right)
	jsonFmt := formatter.NewDeltaFormatter()

	diff := gojsondiff.New().CompareObjects(left, right)
	jsonDiff, _ := jsonFmt.Format(diff)

	asciiFmt := formatter.NewAsciiFormatter(left, formatter.AsciiFormatterConfig{Coloring: terminal.IsTerminal(int(os.Stdout.Fd()))})
	formatted, _ := asciiFmt.Format(diff)
	println(formatted)

	diffUnmarshaller := gojsondiff.NewUnmarshaller()
	diff, _ = diffUnmarshaller.UnmarshalString(jsonDiff)

	formatted, _ = asciiFmt.Format(diff)
	println(formatted)
}

Program prints ascii formatted diff before and after deserialization. Output below demonstrates that after deserialization string field value is printed as null.

 {
   "data": {
-    "int": 1,
+    "int": 2,
-    "string": "gcr.io/heptio-images/ks-guestbook-demo:0.1"
+    "string": "gcr.io/heptio-images/ks-guestbook-demo:0.2"
   }
 }

 {
   "data": {
-    "int": 1,
+    "int": 2,
-    "string": null
+    "string": null
   }
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant