Skip to content

Commit

Permalink
fixes #1333: apoc.graph.fromDocument should consider mappings field i…
Browse files Browse the repository at this point in the history
…nto document whitelist (#1334)
  • Loading branch information
conker84 authored and sarmbruster committed Nov 13, 2019
1 parent b2889bd commit 9afa563
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/main/java/apoc/graph/util/GraphsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,20 @@ public List<String> labelsForPath(String path) {
}

public List<String> propertiesForPath(String path) {
return mappings.getOrDefault(path, GraphMapping.EMPTY).getProperties();
if (allPropertiesForPath(path)) {
return Collections.emptyList();
}
// We also need to consider the properties defined in the mapping fields
final List<String> pathProperties = mappings.keySet()
.stream()
.filter(key -> key.startsWith(path))
.map(key -> path.length() >= key.length() ? "" : key.substring(path.length() + 1))
.map(key -> key.split("\\.")[0])
.filter(key -> !key.isEmpty())
.collect(Collectors.toList());
List<String> properties = mappings.getOrDefault(path, GraphMapping.EMPTY).getProperties();
properties.addAll(pathProperties);
return properties;
}

public boolean allPropertiesForPath(String path) {
Expand Down
66 changes: 66 additions & 0 deletions src/test/java/apoc/graph/GraphsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -899,4 +899,70 @@ public void testDeeplyNestedStructures() throws IOException {
assertEquals(5, relMap.get("(Worker)-[COMPANY]-(Company)").size());
});
}

@Test
public void testIncludeMappingsAsProperties() throws IOException {
Map<String, Object> json = map("id", 1,
"text", "Text",
"data", "02-11-2019",
"user", map("id", 1, "screenName", "conker84"),
"geo", map("latitude", 11.45, "longitude", -12.3));
TestUtil.testResult(db, "CALL apoc.graph.fromDocument({json}, {config}) yield graph",
map("json", json, "config",
map("skipValidation", false, "mappings", map("$", "Tweet{!id, text}", "$.user", "User{!id, screenName}"))), result -> {
Map<String, Object> map = result.next();
assertEquals("Graph", ((Map) map.get("graph")).get("name"));
Collection<Node> nodes = (Collection<Node>) ((Map) map.get("graph")).get("nodes");
Map<String, List<Node>> nodeMap = nodes.stream()
.collect(Collectors.groupingBy(e -> e.getLabels().iterator().next().name()));

final List<Node> tweets = nodeMap.getOrDefault("Tweet", Collections.emptyList());
assertEquals(1, tweets.size());
assertEquals(new HashSet<>(Arrays.asList("id", "text")), tweets.get(0).getAllProperties().keySet());
final List<Node> users = nodeMap.getOrDefault("User", Collections.emptyList());
assertEquals(1, users.size());
assertEquals(new HashSet<>(Arrays.asList("id","screenName")), users.get(0).getAllProperties().keySet());

Collection<Relationship> rels = (Collection<Relationship>) ((Map) map.get("graph")).get("relationships");
Map<String, List<Relationship>> relMap = rels.stream()
.collect(Collectors.groupingBy(e -> String.format("(%s)-[%s]-(%s)",
e.getStartNode().getLabels().iterator().next().name(),
e.getType().name(),
e.getEndNode().getLabels().iterator().next().name())));
assertEquals(1, relMap.get("(Tweet)-[USER]-(User)").size());
});
}

@Test
public void testValidationCustomIdAsProperties() throws IOException {
Map<String, Object> json = map("id", 1,
"text", "Text",
"data", "02-11-2019",
"user", map("id", 1, "screenName", "conker84"),
"geo", map("latitude", 11.45, "longitude", -12.3));
TestUtil.testResult(db, "CALL apoc.graph.fromDocument({json}, {config}) yield graph",
map("json", json, "config",
map("skipValidation", false, "idField", "foo", "mappings", map("$", "Tweet{!id, text}", "$.user", "User{!screenName}"))), result -> {
Map<String, Object> map = result.next();
assertEquals("Graph", ((Map) map.get("graph")).get("name"));
Collection<Node> nodes = (Collection<Node>) ((Map) map.get("graph")).get("nodes");
Map<String, List<Node>> nodeMap = nodes.stream()
.collect(Collectors.groupingBy(e -> e.getLabels().iterator().next().name()));

final List<Node> tweets = nodeMap.getOrDefault("Tweet", Collections.emptyList());
assertEquals(1, tweets.size());
assertEquals(new HashSet<>(Arrays.asList("id", "text")), tweets.get(0).getAllProperties().keySet());
final List<Node> users = nodeMap.getOrDefault("User", Collections.emptyList());
assertEquals(1, users.size());
assertEquals(new HashSet<>(Arrays.asList("screenName")), users.get(0).getAllProperties().keySet());

Collection<Relationship> rels = (Collection<Relationship>) ((Map) map.get("graph")).get("relationships");
Map<String, List<Relationship>> relMap = rels.stream()
.collect(Collectors.groupingBy(e -> String.format("(%s)-[%s]-(%s)",
e.getStartNode().getLabels().iterator().next().name(),
e.getType().name(),
e.getEndNode().getLabels().iterator().next().name())));
assertEquals(1, relMap.get("(Tweet)-[USER]-(User)").size());
});
}
}

0 comments on commit 9afa563

Please # to comment.