From b069e2bfd51b3015d29452aeddda6059cff6bc0f Mon Sep 17 00:00:00 2001 From: Giuseppe Villani Date: Tue, 2 Aug 2022 14:37:38 +0200 Subject: [PATCH] Fixes #2992: apoc.diff.nodes() returns list-type properties as different when they are the same (#3083) * apoc.diff.nodes() returns list-type properties as different when they are the same * code clean --- core/src/main/java/apoc/diff/Diff.java | 11 +++++++---- core/src/test/java/apoc/diff/DiffTest.java | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/apoc/diff/Diff.java b/core/src/main/java/apoc/diff/Diff.java index e4bc5f423e..53c82fd9ec 100644 --- a/core/src/main/java/apoc/diff/Diff.java +++ b/core/src/main/java/apoc/diff/Diff.java @@ -10,6 +10,8 @@ import java.util.HashMap; import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; /** * @author Benjamin Clauss @@ -45,9 +47,10 @@ private Map getPropertiesOnlyLeft(Map left, Map< } private Map getPropertiesInCommon(Map left, Map right) { - Map inCommon = new HashMap<>(left); - inCommon.entrySet().retainAll(right.entrySet()); - return inCommon; + // we use filter(entry -> Objects.deepEquals...) instead of retainAll because retainAll use `.equals()`, so it doesn't compare arrays + 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> getPropertiesDiffering(Map left, Map right) { @@ -57,7 +60,7 @@ private Map> getPropertiesDiffering(Map 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 pairs = new HashMap<>(); pairs.put("left", left.get(entry.getKey())); pairs.put("right", right.get(entry.getKey())); diff --git a/core/src/test/java/apoc/diff/DiffTest.java b/core/src/test/java/apoc/diff/DiffTest.java index 35b1a503ee..ba7fa0704b 100644 --- a/core/src/test/java/apoc/diff/DiffTest.java +++ b/core/src/test/java/apoc/diff/DiffTest.java @@ -13,6 +13,7 @@ import org.neo4j.test.rule.ImpermanentDbmsRule; import java.util.HashMap; +import java.util.List; import java.util.Map; import static org.junit.Assert.*; @@ -52,6 +53,24 @@ public static void setup() throws Exception { } } + @Test + public void nodesWithList() { + final List 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> res = (Map>) r.get("result"); + Map 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 result =