Skip to content

Commit 5870877

Browse files
committed
#383 - Fix Criteria mapping when composing a group from top-level criteria.
Using Criteria.from(…) with multiple Criteria objects now uses properly AND combination along with group nesting to render a correct criteria. Previously, the INITIAL combinator in groups caused a mapping exception.
1 parent bd106a9 commit 5870877

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/main/java/org/springframework/data/r2dbc/query/QueryMapper.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,14 @@ private Condition combine(CriteriaDefinition criteria, @Nullable Condition curre
322322

323323
if (currentCondition == null) {
324324
currentCondition = nextCondition;
325+
} else if (combinator == CriteriaDefinition.Combinator.INITIAL) {
326+
currentCondition = currentCondition.and(Conditions.nest(nextCondition));
325327
} else if (combinator == CriteriaDefinition.Combinator.AND) {
326328
currentCondition = currentCondition.and(nextCondition);
327329
} else if (combinator == CriteriaDefinition.Combinator.OR) {
328330
currentCondition = currentCondition.or(nextCondition);
329331
} else {
330-
throw new IllegalStateException("Combinator " + criteria.getCombinator() + " not supported");
332+
throw new IllegalStateException("Combinator " + combinator + " not supported");
331333
}
332334

333335
return currentCondition;

src/test/java/org/springframework/data/r2dbc/query/QueryMapperUnitTests.java

+16
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,22 @@ public void shouldMapFrom() {
126126
.isEqualTo("person.name = ?[$1] AND (person.name = ?[$2] OR person.age < ?[$3])");
127127
}
128128

129+
@Test // gh-383
130+
public void shouldMapFromConcat() {
131+
132+
Criteria criteria = Criteria.from(Criteria.where("name").is("Foo"), Criteria.where("name").is("Bar") //
133+
.or("age").lessThan(49));
134+
135+
assertThat(map(criteria).getCondition().toString())
136+
.isEqualTo("(person.name = ?[$1] AND (person.name = ?[$2] OR person.age < ?[$3]))");
137+
138+
criteria = Criteria.from(Criteria.where("name").is("Foo"), Criteria.where("name").is("Bar") //
139+
.or("age").lessThan(49), Criteria.where("foo").is("bar"));
140+
141+
assertThat(map(criteria).getCondition().toString())
142+
.isEqualTo("(person.name = ?[$1] AND (person.name = ?[$2] OR person.age < ?[$3]) AND (person.foo = ?[$4]))");
143+
}
144+
129145
@Test // gh-64
130146
public void shouldMapSimpleCriteria() {
131147

0 commit comments

Comments
 (0)