Skip to content

Commit c2386b6

Browse files
mp911deschauder
authored andcommitted
DATAJDBC-539 - Fix parameter wrapping for IN criteria.
We now no longer double-wrap parameters for IN criteria. Previously, collection arguments were wrapped into another collection which caused double-wrapped lists. Original pull request: #217.
1 parent 4ee9eb8 commit c2386b6

File tree

2 files changed

+118
-2
lines changed

2 files changed

+118
-2
lines changed

spring-data-relational/src/main/java/org/springframework/data/relational/repository/query/CriteriaFactory.java

+20-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
*/
1616
package org.springframework.data.relational.repository.query;
1717

18+
import java.util.Collection;
19+
import java.util.Collections;
20+
1821
import org.springframework.data.relational.core.query.Criteria;
1922
import org.springframework.data.relational.core.sql.Expression;
2023
import org.springframework.data.repository.query.parser.Part;
2124
import org.springframework.util.Assert;
25+
import org.springframework.util.CollectionUtils;
2226

2327
/**
2428
* Simple factory to contain logic to create {@link Criteria}s from {@link Part}s.
@@ -90,8 +94,8 @@ public Criteria createCriteria(Part part) {
9094
case IN:
9195
case NOT_IN: {
9296
ParameterMetadata paramMetadata = parameterMetadataProvider.next(part);
93-
Criteria criteria = part.getType() == Part.Type.IN ? criteriaStep.in(paramMetadata.getValue())
94-
: criteriaStep.notIn(paramMetadata.getValue());
97+
Criteria criteria = part.getType() == Part.Type.IN ? criteriaStep.in(asCollection(paramMetadata.getValue()))
98+
: criteriaStep.notIn(asCollection(paramMetadata.getValue()));
9599
return criteria.ignoreCase(shouldIgnoreCase(part) && checkCanUpperCase(part, part.getProperty().getType()));
96100
}
97101
case STARTING_WITH:
@@ -163,4 +167,18 @@ private boolean checkCanUpperCase(Part part, Class<?>... expressionTypes) {
163167
private boolean canUpperCase(Class<?> expressionType) {
164168
return expressionType == String.class;
165169
}
170+
171+
@SuppressWarnings("unchecked")
172+
private static Collection<Object> asCollection(Object value) {
173+
174+
if (value instanceof Collection) {
175+
return (Collection<Object>) value;
176+
}
177+
178+
if (value.getClass().isArray()) {
179+
return CollectionUtils.arrayToList(value);
180+
}
181+
182+
return Collections.singletonList(value);
183+
}
166184
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2020 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.relational.repository.query;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import lombok.Data;
21+
import lombok.SneakyThrows;
22+
23+
import java.lang.reflect.Method;
24+
import java.util.Arrays;
25+
import java.util.List;
26+
27+
import org.junit.jupiter.api.Test;
28+
29+
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
30+
import org.springframework.data.relational.core.query.Criteria;
31+
import org.springframework.data.repository.Repository;
32+
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
33+
import org.springframework.data.repository.query.QueryMethod;
34+
import org.springframework.data.repository.query.parser.Part;
35+
36+
/**
37+
* Unit tests for {@link CriteriaFactory}.
38+
*
39+
* @author Mark Paluch
40+
*/
41+
public class CriteriaFactoryUnitTests {
42+
43+
@Test // DATAJDBC-539
44+
void shouldConsiderIterableValuesInInOperator() {
45+
46+
QueryMethod queryMethod = getQueryMethod("findAllByNameIn", List.class);
47+
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, Arrays.asList("foo", "bar"));
48+
ParameterMetadataProvider parameterMetadata = new ParameterMetadataProvider(accessor);
49+
CriteriaFactory criteriaFactory = new CriteriaFactory(parameterMetadata);
50+
51+
Part part = new Part("NameIn", User.class);
52+
53+
Criteria criteria = criteriaFactory.createCriteria(part);
54+
55+
assertThat(criteria.getValue()).isEqualTo(Arrays.asList("foo", "bar"));
56+
}
57+
58+
@Test // DATAJDBC-539
59+
void shouldConsiderArrayValuesInInOperator() {
60+
61+
QueryMethod queryMethod = getQueryMethod("findAllByNameIn", String[].class);
62+
63+
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod,
64+
new Object[] { new String[] { "foo", "bar" } });
65+
ParameterMetadataProvider parameterMetadata = new ParameterMetadataProvider(accessor);
66+
CriteriaFactory criteriaFactory = new CriteriaFactory(parameterMetadata);
67+
68+
Part part = new Part("NameIn", User.class);
69+
70+
Criteria criteria = criteriaFactory.createCriteria(part);
71+
72+
assertThat(criteria.getValue()).isEqualTo(Arrays.asList("foo", "bar"));
73+
}
74+
75+
@SneakyThrows
76+
private QueryMethod getQueryMethod(String methodName, Class<?>... parameterTypes) {
77+
Method method = UserRepository.class.getMethod(methodName, parameterTypes);
78+
return new QueryMethod(method, new DefaultRepositoryMetadata(UserRepository.class),
79+
new SpelAwareProxyProjectionFactory());
80+
}
81+
82+
private RelationalParametersParameterAccessor getAccessor(QueryMethod queryMethod, Object... values) {
83+
return new RelationalParametersParameterAccessor(queryMethod, values);
84+
}
85+
86+
interface UserRepository extends Repository<User, Long> {
87+
88+
User findAllByNameIn(List<String> names);
89+
90+
User findAllByNameIn(String[] names);
91+
}
92+
93+
@Data
94+
static class User {
95+
96+
String name;
97+
}
98+
}

0 commit comments

Comments
 (0)