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

Correct index passed to DiffInternal for modification detection in LS… #45

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

barry-bvl
Copy link

Noticed that the diff of arrays was not always correct when items were removed, modified and added.
The modifications where not identified.

example:
Lets assume I want to diff the following
{ "id": "644fe678c3541f5485b760d2", "myArray": [ { "id": "644fe6b2c3541f5485b7639f", "comment": "bogus" }, { "id": "65fa21a122020709ac83b0ca", "comment": "willberemoved" }, { "id": "644febdbc3541f5485b79026", "comment": "foobar" }, { "id": "645001bfc3541f5485b7e189", "comment": "example" }, { "id": "645003fac3541f5485b7f2ab", "comment": "ok" } ] }
with
{ "id": "644fe678c3541f5485b760d2", "myArray": [ { "id": "644fe6b2c3541f5485b7639f", "comment": "bogus" }, { "id": "644febdbc3541f5485b79026", "comment": "foobar" }, { "id": "645001bfc3541f5485b7e189", "comment": "example adapted" }, { "id": "645003fac3541f5485b7f2ab", "comment": "ok" }, { "id": "myid", "comment": "isadded" }, { "id": "myid2", "comment": "isadded2" }
[example, example modified] modification is not found by the diff (delta format).
After some investigation it seems to just pass the wrong index to the DiffInternal in the LCS when checking for modifications in the right side.

After the modification the delta includes the modification as well.

@barry-bvl barry-bvl requested a review from weichch as a code owner March 20, 2024 22:28
@@ -121,7 +121,7 @@ static partial class JsonDiffPatcher

// We have two objects equal by position or other criteria
var itemDiff = new JsonDiffDelta();
DiffInternal(ref itemDiff, left[entry.LeftIndex], right[entry.RightIndex], options);
DiffInternal(ref itemDiff, left[commonHead + entry.LeftIndex], right[commonHead + entry.RightIndex], options);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for digging.

I think this change makes sense. I realized I couldn't reproduce this issue without a custom key finder. Is that what you use? Without a key finder the diff result is correct but not optimal (no modification).

Do you want to add a test for this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed using an array objectkeyfinder, added a specific unittest that covers this usecase.

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

Successfully merging this pull request may close these issues.

None yet

2 participants