Closed DrLansing closed 12 years ago
Exciting! I'll take a look.
I've distilled the problem down to this simple test case.
@Test
public void testAdjustArrayMutationBoundariesWithObjectDeletion() {
String from = "{\"a\":[{\"b\":{\"id\":\"id1\"}},{\"b\":{\"id\":\"id2\"}}]}";
String to = "{\"a\":[{\"b\":{\"id\":\"id2\"}}]}";
String d = JsonDiff.diff(from, to);
String p = JsonPatch.apply(from, d);
Assert.assertEquals(to, p);
}
It's that classic diff algorithm problem you sometimes see in diff -u
when you insert/remove a method and it doesn't know exactly what the addition/removal boundaries are.
From:
/*
* Doc for a
*/
void a () {
}
/*
* Doc for c
*/
void b () {
}
To:
/*
* Doc for a
*/
void a () {
}
/*
* Inserted method
*/
void c () {
}
/*
* Doc for c
*/
void b () {
}
Diff:
prince$ diff -u a c
--- a 2011-12-13 08:57:06.000000000 +0100
+++ c 2011-12-13 08:56:59.000000000 +0100
@@ -4,6 +4,11 @@
void a () {
}
/*
+ * Inserted method
+ */
+void c () {
+}
+/*
* Doc for c
*/
void b () {
Notice the last added row +/*
. The user obviously didn't think of it that way - and for this diff it makes no difference to the end result. But the way jsondiff uses the algorithm it does.
This is what I try to fix in adjustArrayMutationBoundaries()
- but it's broken when there are nested deep objects.
I need to think on how to solve that.
Oops. That's: <wrapper><a><b><id>id1</id></b></a><a><b><id>id2</id></b></a></wrapper> <wrapper><a><b><id>id2</id></b></a></wrapper> DeleteNode: <a><b><id>id1</id></b></a> path 0:0
A quick update. I'm getting there. The test data you provided uncovered two bugs, one which is solved (the one described above), the other I'm wrestling with. It'll get there.
I haven't give up. It was just harder to find time to look at it. I've checked in my test cases (that are breaking atm). testModifyWhileDeletingPreviousElement1
and testModifyWhileDeletingPreviousElement2
distills the problem down to the core.
Looks good. Couldn't break it.
Good to hear!
Input:
Test:
Analysis: Some data is lost. Compare this part of
more[1]
:to this part of the result: