diff --git a/neo4j-cypher-dsl/src/main/java/org/neo4j/cypherdsl/core/renderer/DefaultVisitor.java b/neo4j-cypher-dsl/src/main/java/org/neo4j/cypherdsl/core/renderer/DefaultVisitor.java index 493d488b47..83452740ae 100644 --- a/neo4j-cypher-dsl/src/main/java/org/neo4j/cypherdsl/core/renderer/DefaultVisitor.java +++ b/neo4j-cypher-dsl/src/main/java/org/neo4j/cypherdsl/core/renderer/DefaultVisitor.java @@ -287,6 +287,22 @@ void enter(With with) { void leave(With with) { builder.append(" "); + clearPreviouslyVisitedNamed(with); + } + + private void clearPreviouslyVisitedNamed(With with) { + // We need to clear the named cache after defining a with. + // Everything not taken into the next step has to go. + // TODO This must be probably nested for subqueries, too + java.util.Set retain = new HashSet<>(); + with.accept(segment -> { + if (segment instanceof SymbolicName) { + visitedNamed.stream() + .filter(named -> named.getRequiredSymbolicName().equals(segment)) + .forEach(retain::add); + } + }); + this.visitedNamed.retainAll(retain); } void enter(Delete delete) { diff --git a/neo4j-cypher-dsl/src/test/java/org/neo4j/cypherdsl/core/CypherIT.java b/neo4j-cypher-dsl/src/test/java/org/neo4j/cypherdsl/core/CypherIT.java index be8ebf604c..5539700f0a 100644 --- a/neo4j-cypher-dsl/src/test/java/org/neo4j/cypherdsl/core/CypherIT.java +++ b/neo4j-cypher-dsl/src/test/java/org/neo4j/cypherdsl/core/CypherIT.java @@ -654,7 +654,7 @@ void mixedClausesWithWith() { assertThat(cypherRenderer.render(statement)) .isEqualTo( - "MATCH (u:`User`)-[:`OWNS`]->(b:`Bike`) MATCH (t:`Trip`) DELETE t WITH b, t MATCH (u) WITH b, u RETURN b, u"); + "MATCH (u:`User`)-[:`OWNS`]->(b:`Bike`) MATCH (t:`Trip`) DELETE t WITH b, t MATCH (u:`User`) WITH b, u RETURN b, u"); } }