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

Incorrect (?) diffpatch when modifying collections #4

Open
hcd opened this issue Nov 13, 2017 · 2 comments
Open

Incorrect (?) diffpatch when modifying collections #4

hcd opened this issue Nov 13, 2017 · 2 comments

Comments

@hcd
Copy link

hcd commented Nov 13, 2017

Hi,

I have an issue when creating diffpatches on my objects after I have modified a sub-collection on those objects. It all works fine as long as I add or remove items to the collection, but not when I both and and remove items. I've create a very simple unit test that demonstrates this. Is this an issue or "expected behavior" ?

Test:

`
public class MyClass
{
public List MyList { get; set; }
}

[TestClass]
public class AddRemoveDiffPatchTests
{
    [TestMethod]
    public void Test()
    {
        var differ = new JsonDiffer();
        var myVar = new MyClass();
        myVar.MyList= new List<string>() { "Value1", "Value2", "Value3", "Value4", "Value5" };
        JToken original = JToken.Parse(myVar.ToJson());
        //first let's remove a value from the list
        myVar.MyList.Remove("Value2");
        JToken modified1 = JToken.Parse(myVar.ToJson());
        var diff1 = differ.Diff(original, modified1);
        Assert.AreEqual(1, diff1.Operations.Count);//correct - 1 "remove" operation
        //now add a value to the list
        myVar.MyList.Add("Value6");
        JToken modified2 = JToken.Parse(myVar.ToJson());
        var diff2 = differ.Diff(modified1, modified2);
        Assert.AreEqual(1, diff2.Operations.Count);//correct - 1 "add" operation
        //now both remove a value and add a value 
        myVar.MyList.Remove("Value3");
        myVar.MyList.Add("Value7");
        JToken modified3 = JToken.Parse(myVar.ToJson());
        var diff3 = differ.Diff(modified2, modified3);
        //i would expect 2 operations, 1 remove and 1 add
        Assert.AreEqual(2, diff3.Operations.Count);//FAILS - there are 7 operations:  3 remove, 1 replace and 3 add


    }

}

`

Kind regards,
Sven.

@mcintyre321
Copy link
Owner

I've had a quick look, and it does seem to produce a patch document that can be used to make the correct final document, but with a highly non-optimal number of operations.

I think that the JsonDiffer probably needs extension to work with strings - I added an LCS algorithm which can produce good results for arrays, but need to be passed a suitable equality comparer.

I've only developed the library enough to work with the use cases I've encountered, but I'm very happy to accept pull requests, and give you some pointers/help if you would like to work on it

@CodeGlitcher
Copy link

We had the same problem, @idobosman has made a fix.
See pr: 11

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

No branches or pull requests

3 participants