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

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

Open
brandonwang15 opened this issue Dec 21, 2022 · 1 comment

Comments

@brandonwang15
Copy link

brandonwang15 commented Dec 21, 2022

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.

@nsf
Copy link
Owner

nsf commented Dec 24, 2022

Yes, I agree with that this behaviour may not be ideal. But that's how it is. To be honest this lib is a very low priority for me and it's unlikely that I'm going to do anything about it anytime soon.

I can see you're using JSON config. This lib was never meant to provide a machine readable output. I don't even recall who contributed JSON config option and I never used it myself and not planning to. When printing out something like "null <= {}" the lib means that values are of different type and further comparison makes no sense. Same applies to arrays vs other values. The value is different already, thus no need to go deeper. That's what I had in mind when implementing it.

Maybe one day will do something about it (unlikely soon).

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

2 participants