Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # 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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private static void DiffArray(

// 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.

if (itemDiff.Document is not null)
{
delta.ArrayChange(i, false, itemDiff);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.JsonDiffPatch;
using System;
using System.Text.Json.JsonDiffPatch;
using System.Text.Json.Nodes;
using Xunit;

Expand Down Expand Up @@ -50,6 +51,47 @@ public void Diff_ArrayMove()
Assert.Equal("{\"_t\":\"a\",\"_0\":[\"\",1,3]}", diff!.ToJsonString());
}

[Fact]
public void Diff_Array_WithArrayObjectItemkeyFinder()
{
var source = "{ \"id\": \"1\", \"myArray\": [ { \"id\": \"2\", \"comment\": \"bogus\" }, { \"id\": \"3\", \"comment\": \"willberemoved\" }, { \"id\": \"4\", \"comment\": \"foobar\" }, { \"id\": \"5\", \"comment\": \"example\" }, { \"id\": \"6\", \"comment\": \"ok\" } ] }";
var modified = "{ \"id\": \"1\", \"myArray\": [ { \"id\": \"2\", \"comment\": \"bogus\" }, { \"id\": \"4\", \"comment\": \"foobar\" }, { \"id\": \"5\", \"comment\": \"example adapted\" }, { \"id\": \"6\", \"comment\": \"ok\" }, { \"id\": \"myid\", \"comment\": \"isadded\" }, { \"id\": \"myid2\", \"comment\": \"isadded2\" }]}";

var left = JsonNode.Parse(source);
var right = JsonNode.Parse(modified);

var options = new JsonDiffOptions
{
ArrayObjectItemKeyFinder = (node, index) =>
{
if (node is JsonObject obj)
{
if (obj.TryGetPropertyValue("id", out var value))
{
try
{
return value?.GetValue<string>() ?? "";
}
catch (InvalidOperationException)
{
return value?.GetValue<int>() ?? 0;
}

}
else if (obj.TryGetPropertyValue("name", out value))
{
return value?.GetValue<string>() ?? "";
}
}
return index; //fallback
}
};

var diff = left.Diff(right, options);

Assert.Equal("{\"myArray\":{\"_t\":\"a\",\"_1\":[{\"id\":\"3\",\"comment\":\"willberemoved\"},0,0],\"2\":{\"comment\":[\"example\",\"example adapted\"]},\"4\":[{\"id\":\"myid\",\"comment\":\"isadded\"}],\"5\":[{\"id\":\"myid2\",\"comment\":\"isadded2\"}]}}", diff!.ToJsonString());
}

[Fact]
public void Diff_NullProperty()
{
Expand Down