Skip to content

Commit 416e8f8

Browse files
committed
fix: error on patchDiffViewer with empty objects
1 parent 1383772 commit 416e8f8

File tree

7 files changed

+14
-2
lines changed

7 files changed

+14
-2
lines changed

src/main/java/com/deblock/jsondiff/diff/JsonObjectDiff.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.deblock.jsondiff.matcher.Path;
44
import com.deblock.jsondiff.viewer.JsonDiffViewer;
55
import com.fasterxml.jackson.databind.JsonNode;
6+
import com.fasterxml.jackson.databind.node.ObjectNode;
67

78
import java.util.HashMap;
89
import java.util.Map;
@@ -74,6 +75,10 @@ public void display(JsonDiffViewer viewer) {
7475
for (final var entry: extraProperties.entrySet()) {
7576
viewer.extraProperty(path().add(Path.PathItem.of(entry.getKey())), entry.getValue());
7677
}
78+
final var isEmptyObject = notFoundProperties.isEmpty() && propertiesDiff.isEmpty() && extraProperties.isEmpty();
79+
if (isEmptyObject) {
80+
viewer.primaryMatching(path(), new ObjectNode(null));
81+
}
7782
}
7883

7984
@Override

src/test/java/com/deblock/jsondiff/matcher/JsonDiffAsserter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void validate(JsonDiff jsonDiff) {
9090
}
9191
jsonDiff.display(this);
9292

93-
final var allErrors = Stream.of(missingPropertyAsserters, nonMatchingPropertyAsserters, matchingPropertyAsserters, primaryNonMatchingAsserters, extraPropertyAsserters)
93+
final var allErrors = Stream.of(primaryMatchingAsserters, missingPropertyAsserters, nonMatchingPropertyAsserters, matchingPropertyAsserters, primaryNonMatchingAsserters, extraPropertyAsserters)
9494
.flatMap(Collection::stream)
9595
.filter(asserter -> !asserter.isDone())
9696
.map(Asserter::getError)

src/test/java/com/deblock/jsondiff/matcher/LenientJsonObjectPartialMatcherTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ void shouldReturnFullMatchForTwoEmptyObjects() {
2222

2323
final var result = new LenientJsonObjectPartialMatcher().jsonDiff(path, object1, object2, null);
2424

25-
assertEquals(100, result.similarityRate());
2625
assertEquals(path, result.path());
26+
new JsonDiffAsserter()
27+
.assertSimilarityRate(60, 40)
28+
.assertPrimaryMatching(path)
29+
.validate(result);
2730
}
2831

2932
@Test

src/test/java/com/deblock/jsondiff/viewer/actual.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"extraObject": {
1313
"a": "1"
1414
},
15+
"emptyObject": {},
1516
"arrayWithObjectWithIssue": [
1617
{
1718
"a": {

src/test/java/com/deblock/jsondiff/viewer/expected.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"a": "1",
1010
"c": "5"
1111
},
12+
"emptyObject": {},
1213
"arrayWithObjectWithIssue": [
1314
{
1415
"a": {

src/test/java/com/deblock/jsondiff/viewer/lenientDiff.patch

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
+ "a": "1",
3737
+ "c": "5"
3838
},
39+
"emptyObject": {},
3940
"arrayWithMissingProperty": [
4041
+ 2,
4142
3

src/test/java/com/deblock/jsondiff/viewer/strictDiff.patch

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
+ "a": "1",
3737
+ "c": "5"
3838
},
39+
"emptyObject": {},
3940
"arrayWithMissingProperty": [
4041
- 3
4142
+ 2,

0 commit comments

Comments
 (0)