yudai / gojsondiff

Go JSON Diff
Other
536 stars 79 forks source link

Incorrect diff when first item of array changed #30

Open jkomoros opened 6 years ago

jkomoros commented 6 years ago

I ran into a case where the diff output appears to be incorrect. What follows is a reduced test case.

Contents of left.json:

{
    "Indexes":[
        2,
        -1,
        1
    ]
}

Contents of right.json:

{
    "Indexes":[
          -1,
          -1,
          -1
    ]
}

Run the following:

jd -f delta left.json right.json > patch.json
jp patch.json left.json > derived_right.json
jd -f delta right.json derived_right.json

(That is, create a diff, then validate that applying the diff to the left input produces the right output.)

Expected output:

{}

(That is, the diff successfully creates right.json)

Actual output:

{
  "Indexes": {
    "_0": [
      -1,
      0,
      0
    ],
    "_t": "a"
  }
}

(That is, derived_right.json is different from right.json)

If you inspect dervied_right.json you see:

{
  "Indexes": [
    -1,
    -1
  ]
}

(That is, derived_right.json is missing one row)

I originally came across this with a much larger json diff, but reduced to this test case with trial and error. Oddly enough, the behavior appears to rely on the values of the integer constants in the left.json. For example, if you flip the position of 2 and 1 in left.json, it works as expected.

Note that I also was seeing similar behavior for an array that was not integer constants, but strings that went from "somevalue" to "" if they were in the first position in the array. I factored them out of the test case because the integer test case still triggered.

I think that the original jsondiffpatch works correctly in this case, at least based on using the webapp version: https://benjamine.github.io/jsondiffpatch/demo/index.html

I'm using the 0.0.2 version of the jd tool

Apologies if I'm missing something obvious or not using this as intended!