josephburnett / jd

JSON diff and patch
MIT License
1.86k stars 51 forks source link

Wrong path in diff output #18

Closed wittdev closed 3 years ago

wittdev commented 5 years ago

File1:

{
    "Reservations": [
        {
            "Instances": [
                {
                    "Tags": [
                        {
                            "Value": "testing",
                            "Key": "Name"
                        },
                        {
                            "Value": "True",
                            "Key": "Infrastructure"
                        }
                    ]
                }
            ]
        }
    ]
}

File2:

{
    "Reservations": [
        {
            "Instances": [
                {
                    "Tags": [
                        {
                            "Value": "testing",
                            "Key": "Name"
                        },
                        {
                            "Value": "QA",
                            "Key": "Class"
                        },
                        {
                            "Value": "True",
                            "Key": "Infrastructure"
                        }
                    ]
                }
            ]
        }
    ]
}

Current behavior (Ubuntu 18.04, go version go1.10.4, latest release of jd):

jd File1 File2

@ ["Reservations",0,"Instances",0,"Tags",2,"Value"]
- "Infrastructure"
+ "Class"
@ ["Reservations",0,"Instances",0,"Tags",2,"Value"]
- "True"
+ "QA"
@ ["Reservations",0,"Instances",0,"Tags",2]
+ {"Key":"Infrastructure","Value":"True"}

Expected/desired output:

@ ["Reservations",0,"Instances",0,"Tags",2,"Key"]
- "Infrastructure"
+ "Class"
@ ["Reservations",0,"Instances",0,"Tags",2,"Value"]
- "True"
+ "QA"
@ ["Reservations",0,"Instances",0,"Tags",2]
+ {"Key":"Infrastructure","Value":"True"}
josephburnett commented 5 years ago

Thanks for the bug report. I wasn't cloning the path when making diff entries, so sometimes the same underlying array was used. So the previous diff was corrupted by the next.

Fixed in https://github.com/josephburnett/jd/commit/0dbacc995392cca5b5f93fc0e570c091c22b13a4. Can you pull from master and try again?