Skip to content

Commit bc2efd8

Browse files
committed
GH-2655 - Fix false-positive cycle detection.
If multiple base class extending entities contain the very same target, it was reported as a cycle. Now it will get correctly detected as a directed graph. Closes #2655
1 parent 302b538 commit bc2efd8

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/main/java/org/springframework/data/neo4j/core/mapping/DefaultNeo4jPersistentEntity.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -588,19 +588,24 @@ private boolean calculatePossibleCircles(Predicate<PropertyFilter.RelaxedPropert
588588
private boolean calculatePossibleCircles(NodeDescription<?> nodeDescription, Set<NodeDescription<?>> visitedNodes, Predicate<PropertyFilter.RelaxedPropertyPath> includeField, PropertyFilter.RelaxedPropertyPath path) {
589589
Collection<RelationshipDescription> relationships = ((DefaultNeo4jPersistentEntity<?>) nodeDescription).getRelationshipsInHierarchy(includeField, path);
590590

591+
Collection<NodeDescription<?>> visitedTargetNodes = new HashSet<>();
591592
for (RelationshipDescription relationship : relationships) {
592593
NodeDescription<?> targetNode = relationship.getTarget();
593594
if (visitedNodes.contains(targetNode)) {
594595
return true;
595596
}
596-
visitedNodes.add(targetNode);
597-
597+
visitedTargetNodes.add(targetNode);
598598
// Branch out again for the sub-tree with all previously visited nodes
599599
Set<NodeDescription<?>> branchedVisitedNodes = new HashSet<>(visitedNodes);
600+
// Add the already visited target nodes for the next level,
601+
// but don't (!) add them to the visitedNodes yet.
602+
// Otherwise, the same "parallel" defined target nodes will report a false circle.
603+
branchedVisitedNodes.addAll(visitedTargetNodes);
600604
if (calculatePossibleCircles(targetNode, branchedVisitedNodes, includeField, path.append(relationship.getFieldName()))) {
601605
return true;
602606
}
603607
}
608+
visitedNodes.addAll(visitedTargetNodes);
604609
return false;
605610
}
606611
}

0 commit comments

Comments
 (0)