jsondiff icon indicating copy to clipboard operation
jsondiff copied to clipboard

Bug when second list is empty

Open KKomarov opened this issue 7 years ago • 3 comments

Kind of a bug:

>>> import jsondiff
>>> a = []
>>> b = ['a', 'b', 'c']
>>> jsondiff.diff(a,b)
['a', 'b', 'c']  # ok
>>> jsondiff.diff(b,a)
[]  # wrong. expected: {delete: [3, 2, 1]}
>>> jsondiff.diff(b,a, syntax='explicit')
[]  # wrong. expected: {delete: [3, 2, 1]}
>>> jsondiff.diff(a,b, syntax='symmetric')
[[], ['a', 'b', 'c']]  # ok ?

KKomarov avatar Mar 29 '17 20:03 KKomarov

Hello @KKomarov,

Please check out #4 for some insight into this behaviour. In this case, jsondiff considers the two lists to be completely different therefore represents the difference as a substitution rather than a patch. It is perhaps debatable whether this is the best default behaviour in the explicit case, but it's a design choice not a bug.

Hope this helps.

ericremoreynolds avatar Apr 03 '17 18:04 ericremoreynolds

@ericremoreynolds Thank you, I agree there is no problem with symmetric syntax. But others syntaxes have problems.

# another example
>>> from jsondiff import diff
>>> diff(['a', 'b', 'c'], ['a', 'c'])
{delete: [1]}
>>> diff(['a', 'b', 'c'], ['a'])
{delete: [2, 1]}
>>> diff(['a', 'b', 'c'], [])  # bug is here
[]

KKomarov avatar Apr 03 '17 20:04 KKomarov

Adding to this, jsondiff unexpectedly returned a list when one of the comparitor is an empty dict.

>>> jsondiff.diff({'a': 30},{},syntax='symmetric')
[{'a': 30}, {}]
>>> jsondiff.diff({},{'a': 20},syntax='symmetric')
[{}, {'a': 20}]

dodysw avatar Jan 28 '18 16:01 dodysw