Browse Source

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
pull/2684/head
Mark Paluch 4 years ago committed by Christoph Strobl
parent
commit
9e94e110e4
No known key found for this signature in database
GPG Key ID: 8CC1AB53391458C8
  1. 13
      src/main/java/org/springframework/data/querydsl/binding/QuerydslPredicateBuilder.java
  2. 15
      src/test/java/org/springframework/data/querydsl/binding/QuerydslPredicateBuilderUnitTests.java

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

@ -175,30 +175,29 @@ public class QuerydslPredicateBuilder { @@ -175,30 +175,29 @@ public class QuerydslPredicateBuilder {
*/
private Collection<Object> convertToPropertyPathSpecificType(List<?> source, PathInformation path) {
Class<?> targetType = path.getLeafType();
if (source.isEmpty() || isSingleElementCollectionWithEmptyItem(source)) {
return Collections.emptyList();
}
TypeDescriptor targetType = getTargetTypeDescriptor(path);
Collection<Object> target = new ArrayList<>(source.size());
for (Object value : source) {
target.add(getValue(path, targetType, value));
target.add(getValue(targetType, value));
}
return target;
}
@Nullable
private Object getValue(PathInformation path, Class<?> targetType, Object value) {
private Object getValue(TypeDescriptor targetType, Object value) {
if (ClassUtils.isAssignableValue(targetType, value)) {
if (ClassUtils.isAssignableValue(targetType.getType(), value)) {
return value;
}
if (conversionService.canConvert(value.getClass(), targetType)) {
return conversionService.convert(value, TypeDescriptor.forObject(value), getTargetTypeDescriptor(path));
if (conversionService.canConvert(value.getClass(), targetType.getType())) {
return conversionService.convert(value, TypeDescriptor.forObject(value), targetType);
}
return value;

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

@ -19,10 +19,12 @@ import static org.assertj.core.api.Assertions.*; @@ -19,10 +19,12 @@ import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assumptions.*;
import static org.springframework.test.util.ReflectionTestUtils.*;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.data.querydsl.Address;
import org.springframework.data.querydsl.QSpecialUser;
import org.springframework.data.querydsl.QUser;
@ -31,7 +33,6 @@ import org.springframework.data.querydsl.SimpleEntityPathResolver; @@ -31,7 +33,6 @@ import org.springframework.data.querydsl.SimpleEntityPathResolver;
import org.springframework.data.querydsl.User;
import org.springframework.data.querydsl.UserWrapper;
import org.springframework.data.querydsl.Users;
// import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.data.util.Version;
import org.springframework.format.support.DefaultFormattingConversionService;
@ -181,6 +182,18 @@ class QuerydslPredicateBuilderUnitTests { @@ -181,6 +182,18 @@ class QuerydslPredicateBuilderUnitTests {
assertThat(constant.getConstant()).isEqualTo("rivers,two");
}
@Test
void resolvesCommaSeparatedArgumentToListCorrectly() {
values.add("nickNames", "Walt,Heisenberg");
var predicate = builder.getPredicate(USER_TYPE, values, DEFAULT_BINDINGS);
var constant = (Constant<Object>) ((List<?>) getField(getField(predicate, "mixin"), "args")).get(0);
assertThat(constant.getConstant()).isEqualTo(Arrays.asList("Walt", "Heisenberg"));
}
@Test // DATACMNS-883
void automaticallyInsertsAnyStepInCollectionReference() {

Loading…
Cancel
Save