Skip to content

Difference is invalid/misleading when comparing a 'null' JSON value and a nested JSON value (array / object) #21

@brandonwang15

Description

@brandonwang15

Toy code:

Run in playground: https://go.dev/play/p/XGfPuf_Px8a

	j := jsondiff.DefaultJSONOptions()
	a := `{"foo":null}`
	b := `{"foo":["bar"]}`

	_, diff := jsondiff.Compare([]byte(a), []byte(b), &j)
	fmt.Println("comparing null to array is broken")
	fmt.Println(diff)

	a = `{"foo":null}`
	b = `{"foo":{"value": "bar"}}`

	_, diff = jsondiff.Compare([]byte(a), []byte(b), &j)
	fmt.Println("comparing null to object is broken")
	fmt.Println(diff)

	a = `{"foo":null}`
	b = `{"foo":"bar"}`

	_, diff = jsondiff.Compare([]byte(a), []byte(b), &j)
	fmt.Println("comparing null to a non-nested value works!")
	fmt.Println(diff)
comparing null to array is broken
{
    "foo": {"changed":[null, []]}
}
comparing null to object is broken
{
    "foo": {"changed":[null, {}]}
}
comparing null to a non-nested value works!
{
    "foo": {"changed":[null, "bar"]}
}

If a field has a json 'null' value in either of the JSON objects, and the other value is a nested value (array or object), the comparison fails to print out the correct value for the other object field's non-null value. See the playground example above for an illustration.

One way to hack a fix is convert all nil slice values to empty slice values in the struct before serializing to json.

On second glance, it looks like the more general issue is with comparing any JSON values of different type: https://go.dev/play/p/fo0aUPTuAB8.

It would be nice if in this case, we emitted the string-fied value of the types, instead of the zero type.

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