Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #2992: apoc.diff.nodes() returns list-type properties as different when they are the same #3083

Merged
merged 2 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 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,10 @@ 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;
// 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<String, Map<String, Object>> getPropertiesDiffering(Map<String, Object> left, Map<String, Object> right) {
Expand All @@ -57,7 +60,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
19 changes: 19 additions & 0 deletions core/src/test/java/apoc/diff/DiffTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -52,6 +53,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