Skip to content

Commit

Permalink
Fixes neo4j-contrib#2992: apoc.diff.nodes() returns list-type propert…
Browse files Browse the repository at this point in the history
…ies as different when they are the same
  • Loading branch information
vga91 committed Aug 1, 2022
1 parent 7ea132a commit 51fe8a8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
10 changes: 6 additions & 4 deletions core/src/main/java/apoc/diff/Diff.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author Benjamin Clauss
Expand Down Expand Up @@ -45,9 +47,9 @@ private Map<String, Object> getPropertiesOnlyLeft(Map<String, Object> left, Map<
}

private Map<String, Object> getPropertiesInCommon(Map<String, Object> left, Map<String, Object> right) {
Map<String, Object> inCommon = new HashMap<>(left);
inCommon.entrySet().retainAll(right.entrySet());
return inCommon;
return left.entrySet().stream()
.filter(entry -> Objects.deepEquals(entry.getValue(), right.get(entry.getKey())))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

private Map<String, Map<String, Object>> getPropertiesDiffering(Map<String, Object> left, Map<String, Object> right) {
Expand All @@ -57,7 +59,7 @@ private Map<String, Map<String, Object>> getPropertiesDiffering(Map<String, Obje
keyPairs.keySet().retainAll(right.keySet());

for (Map.Entry<String, Object> entry : keyPairs.entrySet()) {
if (!left.get(entry.getKey()).equals(right.get(entry.getKey()))) {
if (!Objects.deepEquals(left.get(entry.getKey()), right.get(entry.getKey()))) {
Map<String, Object> pairs = new HashMap<>();
pairs.put("left", left.get(entry.getKey()));
pairs.put("right", right.get(entry.getKey()));
Expand Down
21 changes: 21 additions & 0 deletions core/src/test/java/apoc/diff/DiffTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
import org.neo4j.test.rule.DbmsRule;
import org.neo4j.test.rule.ImpermanentDbmsRule;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.*;
Expand Down Expand Up @@ -52,6 +55,24 @@ public static void setup() throws Exception {
}
}

@Test
public void nodesWithList() {
final List<String> list = List.of("tomatoes", "bread", "cookies");
TestUtil.testCall(db, "CREATE (charlie:Person $propCharlie), (hannah:Person $propHanna)\n" +
"RETURN apoc.diff.nodes(charlie, hannah) AS result",
Map.of("propCharlie", Map.of("name", "Charlie", "alpha", "one", "born", 1999, "grocery_list", list),
"propHanna", Map.of("name", "Hannah", "beta", "two", "born", 1999, "grocery_list", list)),
r -> {
Map<String, Map<String, Object>> res = (Map<String, Map<String, Object>>) r.get("result");
Map<String, Object> inCommon = res.get("inCommon");
assertArrayEquals(list.toArray(), (String[]) inCommon.get("grocery_list"));
assertEquals(1999, inCommon.get("born"));
assertEquals(Map.of("alpha", "one"), res.get("leftOnly"));
assertEquals(Map.of("beta", "two"), res.get("rightOnly"));
assertEquals(Map.of("name", Map.of("left", "Charlie", "right", "Hannah")), res.get("different"));
});
}

@Test
public void nodesSame() {
Map<String, Object> result =
Expand Down

0 comments on commit 51fe8a8

Please sign in to comment.