From 51fe8a81b6471d6cb768e02c6125cf12ec79ec3c Mon Sep 17 00:00:00 2001 From: Giuseppe Villani Date: Thu, 16 Jun 2022 10:05:59 +0200 Subject: [PATCH] Fixes #2992: apoc.diff.nodes() returns list-type properties as different when they are the same --- core/src/main/java/apoc/diff/Diff.java | 10 ++++++---- core/src/test/java/apoc/diff/DiffTest.java | 21 +++++++++++++++++++++ 2 files changed, 27 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..bafd2d62e8 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,9 @@ 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; + 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 +59,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..b4bf4a55ff 100644 --- a/core/src/test/java/apoc/diff/DiffTest.java +++ b/core/src/test/java/apoc/diff/DiffTest.java @@ -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.*; @@ -52,6 +55,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 =