Skip to content

Commit e767c68

Browse files
GH-2498 - Apply actual parameters to the nested queries.
This fixes #2498.
1 parent db4a7e5 commit e767c68

File tree

5 files changed

+138
-3
lines changed

5 files changed

+138
-3
lines changed

src/main/java/org/springframework/data/neo4j/core/Neo4jTemplate.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@ private NodesAndRelationshipsByIdStatementProvider createNodesAndRelationshipsBy
10911091
usedParameters = new HashMap<>(parameters);
10921092
usedParameters.putAll(statement.getParameters());
10931093
neo4jClient.query(renderer.render(statement))
1094-
.bindAll(parameters)
1094+
.bindAll(usedParameters)
10951095
.fetch()
10961096
.one()
10971097
.ifPresent(iterateAndMapNextLevel(relationshipIds, relatedNodeIds, relationshipDescription, PropertyPathWalkStep.empty()));

src/test/java/org/springframework/data/neo4j/integration/issues/gh2498/GH2498IT.java

+19-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
*/
1616
package org.springframework.data.neo4j.integration.issues.gh2498;
1717

18+
import static org.assertj.core.api.Assertions.assertThat;
19+
1820
import java.util.Arrays;
1921
import java.util.Collection;
2022
import java.util.List;
2123

22-
import org.assertj.core.api.Assertions;
2324
import org.junit.jupiter.api.BeforeEach;
2425
import org.junit.jupiter.api.Test;
2526
import org.neo4j.cypherdsl.core.Condition;
@@ -59,6 +60,10 @@ void setupData(@Autowired Driver driver, @Autowired BookmarkCapture bookmarkCapt
5960
session.writeTransaction(tx -> tx.run(
6061
"UNWIND ['A', 'B', 'C'] AS name WITH name CREATE (n:DomainModel {id: randomUUID(), name: name})")
6162
.consume());
63+
session.writeTransaction(tx -> tx.run("MATCH (n:Person) DETACH DELETE n").consume());
64+
session.writeTransaction(tx -> tx.run(
65+
"CREATE (n:Person {name: 'a'}) -[:KNOWS] ->(m:Person {name: 'b'})")
66+
.consume());
6267
bookmarkCapture.seedWith(session.lastBookmark());
6368
}
6469
}
@@ -71,11 +76,23 @@ void cypherdslConditionExecutorShouldWorkWithAnonParameters(@Autowired DomainMod
7176
Parameter<List<String>> parameters = Cypher.anonParameter(Arrays.asList("A", "C"));
7277
Condition in = name.in(parameters);
7378
Collection<DomainModel> result = repository.findAll(in, Cypher.sort(name).descending());
74-
Assertions.assertThat(result).hasSize(2)
79+
assertThat(result).hasSize(2)
7580
.map(DomainModel::getName)
7681
.containsExactly("C", "A");
7782
}
7883

84+
@Test // GH-2498
85+
void cypherdslConditionExecutorMustApplyParametersToNestedStatementsToo(@Autowired PersonRepository repository) {
86+
Node personNode = Cypher.node("Person").named("person");
87+
Property name = personNode.property("name");
88+
Parameter<List<String>> param = Cypher.anonParameter(Arrays.asList("a", "b"));
89+
Condition in = name.in(param);
90+
Collection<Person> people = repository.findAll(in);
91+
assertThat(people)
92+
.extracting(Person::getName)
93+
.containsExactlyInAnyOrder("a", "b");
94+
}
95+
7996
@Configuration
8097
@EnableTransactionManagement
8198
@EnableNeo4jRepositories
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2011-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.neo4j.integration.issues.gh2498;
17+
18+
import lombok.AllArgsConstructor;
19+
import lombok.Builder;
20+
import lombok.Getter;
21+
import lombok.NoArgsConstructor;
22+
import lombok.Setter;
23+
24+
import org.springframework.data.neo4j.core.schema.GeneratedValue;
25+
import org.springframework.data.neo4j.core.schema.Id;
26+
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
27+
import org.springframework.data.neo4j.core.schema.TargetNode;
28+
29+
/**
30+
* @author Michael J. Simons
31+
*/
32+
@RelationshipProperties
33+
@Getter
34+
@Setter
35+
@NoArgsConstructor
36+
@AllArgsConstructor
37+
@Builder(toBuilder = true)
38+
public class Know {
39+
@Id
40+
@GeneratedValue
41+
Long id;
42+
@TargetNode
43+
Person person;
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2011-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.neo4j.integration.issues.gh2498;
17+
18+
import lombok.AllArgsConstructor;
19+
import lombok.Builder;
20+
import lombok.Getter;
21+
import lombok.NoArgsConstructor;
22+
import lombok.Setter;
23+
24+
import java.util.List;
25+
26+
import org.springframework.data.neo4j.core.schema.GeneratedValue;
27+
import org.springframework.data.neo4j.core.schema.Id;
28+
import org.springframework.data.neo4j.core.schema.Node;
29+
import org.springframework.data.neo4j.core.schema.Relationship;
30+
31+
/**
32+
* @author Michael J. Simons
33+
*/
34+
@Getter
35+
@Setter
36+
@NoArgsConstructor
37+
@AllArgsConstructor
38+
@Node("Person")
39+
@Builder(toBuilder = true)
40+
public class Person {
41+
@Id
42+
@GeneratedValue
43+
Long id;
44+
String name;
45+
@Relationship(type = "KNOW", direction = Relationship.Direction.INCOMING)
46+
List<Know> knows;
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2011-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.neo4j.integration.issues.gh2498;
17+
18+
import org.springframework.data.neo4j.repository.Neo4jRepository;
19+
import org.springframework.data.neo4j.repository.support.CypherdslConditionExecutor;
20+
import org.springframework.stereotype.Repository;
21+
22+
/**
23+
* @author Michael J. Simons
24+
*/
25+
@Repository
26+
public interface PersonRepository extends Neo4jRepository<Person, Long>, CypherdslConditionExecutor<Person> {
27+
}

0 commit comments

Comments
 (0)