Skip to content

Commit 72d6101

Browse files
mp911dechristophstrobl
authored andcommitted
Fix argument conversion in QuerydslPredicateBuilder.
We now consider the correct argument type instead of checking assignability of the actual property type against the input value. Closes: #2649 Original Pull Request: #2650
1 parent 275834b commit 72d6101

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/main/java/org/springframework/data/querydsl/binding/QuerydslPredicateBuilder.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,30 +176,29 @@ private Path<?> getPath(PathInformation path, QuerydslBindings bindings) {
176176
*/
177177
private Collection<Object> convertToPropertyPathSpecificType(List<?> source, PathInformation path) {
178178

179-
Class<?> targetType = path.getLeafType();
180-
181179
if (source.isEmpty() || isSingleElementCollectionWithEmptyItem(source)) {
182180
return Collections.emptyList();
183181
}
184182

183+
TypeDescriptor targetType = getTargetTypeDescriptor(path);
185184
Collection<Object> target = new ArrayList<>(source.size());
186185

187186
for (Object value : source) {
188-
target.add(getValue(path, targetType, value));
187+
target.add(getValue(targetType, value));
189188
}
190189

191190
return target;
192191
}
193192

194193
@Nullable
195-
private Object getValue(PathInformation path, Class<?> targetType, Object value) {
194+
private Object getValue(TypeDescriptor targetType, Object value) {
196195

197-
if (ClassUtils.isAssignableValue(targetType, value)) {
196+
if (ClassUtils.isAssignableValue(targetType.getType(), value)) {
198197
return value;
199198
}
200199

201-
if (conversionService.canConvert(value.getClass(), targetType)) {
202-
return conversionService.convert(value, TypeDescriptor.forObject(value), getTargetTypeDescriptor(path));
200+
if (conversionService.canConvert(value.getClass(), targetType.getType())) {
201+
return conversionService.convert(value, TypeDescriptor.forObject(value), targetType);
203202
}
204203

205204
return value;

src/test/java/org/springframework/data/querydsl/binding/QuerydslPredicateBuilderUnitTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.springframework.test.util.ReflectionTestUtils.*;
2121

2222
import java.text.ParseException;
23+
import java.util.Arrays;
2324
import java.util.List;
2425

2526
import org.joda.time.DateTime;
@@ -198,6 +199,18 @@ void bindsDateCorrectly() throws ParseException {
198199
assertThat(predicate).isEqualTo(QUser.user.dateOfBirth.eq(format.parseDateTime(date).toDate()));
199200
}
200201

202+
@Test
203+
void resolvesCommaSeparatedArgumentToListCorrectly() {
204+
205+
values.add("nickNames", "Walt,Heisenberg");
206+
207+
Predicate predicate = builder.getPredicate(USER_TYPE, values, DEFAULT_BINDINGS);
208+
209+
Constant<Object> constant = (Constant<Object>) ((List<?>) getField(getField(predicate, "mixin"), "args")).get(0);
210+
211+
assertThat(constant.getConstant()).isEqualTo(Arrays.asList("Walt", "Heisenberg"));
212+
}
213+
201214
@Test // DATACMNS-883
202215
void automaticallyInsertsAnyStepInCollectionReference() {
203216

0 commit comments

Comments
 (0)