Skip to content

Text diff is incorrectly deserialized #31

@alexmt

Description

@alexmt

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
   }
 }

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