Skip to content

Commit 6c0737a

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 a6e0d7b commit 6c0737a

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
@@ -600,19 +600,24 @@ private boolean calculatePossibleCircles(Predicate<PropertyFilter.RelaxedPropert
600600
private boolean calculatePossibleCircles(NodeDescription<?> nodeDescription, Set<NodeDescription<?>> visitedNodes, Predicate<PropertyFilter.RelaxedPropertyPath> includeField, PropertyFilter.RelaxedPropertyPath path) {
601601
Collection<RelationshipDescription> relationships = ((DefaultNeo4jPersistentEntity<?>) nodeDescription).getRelationshipsInHierarchy(includeField, path);
602602

603+
Collection<NodeDescription<?>> visitedTargetNodes = new HashSet<>();
603604
for (RelationshipDescription relationship : relationships) {
604605
NodeDescription<?> targetNode = relationship.getTarget();
605606
if (visitedNodes.contains(targetNode)) {
606607
return true;
607608
}
608-
visitedNodes.add(targetNode);
609-
609+
visitedTargetNodes.add(targetNode);
610610
// Branch out again for the sub-tree with all previously visited nodes
611611
Set<NodeDescription<?>> branchedVisitedNodes = new HashSet<>(visitedNodes);
612+
// Add the already visited target nodes for the next level,
613+
// but don't (!) add them to the visitedNodes yet.
614+
// Otherwise, the same "parallel" defined target nodes will report a false circle.
615+
branchedVisitedNodes.addAll(visitedTargetNodes);
612616
if (calculatePossibleCircles(targetNode, branchedVisitedNodes, includeField, path.append(relationship.getFieldName()))) {
613617
return true;
614618
}
615619
}
620+
visitedNodes.addAll(visitedTargetNodes);
616621
return false;
617622
}
618623
}

0 commit comments

Comments
 (0)