32
32
import org .springframework .data .mapping .PropertyPath ;
33
33
import org .springframework .data .querydsl .EntityPathResolver ;
34
34
import org .springframework .data .util .TypeInformation ;
35
+ import org .springframework .lang .Nullable ;
35
36
import org .springframework .util .Assert ;
37
+ import org .springframework .util .ClassUtils ;
36
38
import org .springframework .util .MultiValueMap ;
37
- import org .springframework .util .StringUtils ;
39
+ import org .springframework .util .ObjectUtils ;
38
40
39
41
import com .querydsl .core .BooleanBuilder ;
40
42
import com .querydsl .core .types .Path ;
@@ -81,8 +83,7 @@ public QuerydslPredicateBuilder(ConversionService conversionService, EntityPathR
81
83
* @param bindings the {@link QuerydslBindings} for the predicate.
82
84
* @return the {@link Predicate}.
83
85
*/
84
- public Predicate getPredicate (TypeInformation <?> type , MultiValueMap <String , String > values ,
85
- QuerydslBindings bindings ) {
86
+ public Predicate getPredicate (TypeInformation <?> type , MultiValueMap <String , ?> values , QuerydslBindings bindings ) {
86
87
87
88
Assert .notNull (bindings , "Context must not be null!" );
88
89
@@ -92,9 +93,9 @@ public Predicate getPredicate(TypeInformation<?> type, MultiValueMap<String, Str
92
93
return getPredicate (builder );
93
94
}
94
95
95
- for (Entry <String , List <String >> entry : values .entrySet ()) {
96
+ for (Entry <String , ? extends List <? >> entry : values .entrySet ()) {
96
97
97
- if (isSingleElementCollectionWithoutText (entry .getValue ())) {
98
+ if (isSingleElementCollectionWithEmptyItem (entry .getValue ())) {
98
99
continue ;
99
100
}
100
101
@@ -165,33 +166,44 @@ private Path<?> getPath(PathInformation path, QuerydslBindings bindings) {
165
166
166
167
/**
167
168
* Converts the given source values into a collection of elements that are of the given {@link PropertyPath}'s type.
168
- * Considers a single element list with an empty {@link String} an empty collection because this basically indicates
169
- * the property having been submitted but no value provided.
169
+ * Considers a single element list with an empty object an empty collection because this basically indicates the
170
+ * property having been submitted but no value provided.
170
171
*
171
172
* @param source must not be {@literal null}.
172
173
* @param path must not be {@literal null}.
173
174
* @return
174
175
*/
175
- private Collection <Object > convertToPropertyPathSpecificType (List <String > source , PathInformation path ) {
176
+ private Collection <Object > convertToPropertyPathSpecificType (List <? > source , PathInformation path ) {
176
177
177
178
Class <?> targetType = path .getLeafType ();
178
179
179
- if (source .isEmpty () || isSingleElementCollectionWithoutText (source )) {
180
+ if (source .isEmpty () || isSingleElementCollectionWithEmptyItem (source )) {
180
181
return Collections .emptyList ();
181
182
}
182
183
183
184
Collection <Object > target = new ArrayList <>(source .size ());
184
185
185
- for (String value : source ) {
186
-
187
- target .add (conversionService .canConvert (String .class , targetType )
188
- ? conversionService .convert (value , TypeDescriptor .forObject (value ), getTargetTypeDescriptor (path ))
189
- : value );
186
+ for (Object value : source ) {
187
+ target .add (getValue (path , targetType , value ));
190
188
}
191
189
192
190
return target ;
193
191
}
194
192
193
+ @ Nullable
194
+ private Object getValue (PathInformation path , Class <?> targetType , Object value ) {
195
+
196
+ if (ClassUtils .isAssignableValue (targetType , value )) {
197
+ return value ;
198
+ }
199
+
200
+ if (conversionService .canConvert (value .getClass (), targetType )) {
201
+ return conversionService .convert (value , TypeDescriptor .forObject (value ), getTargetTypeDescriptor (path ));
202
+ }
203
+
204
+ return value ;
205
+ }
206
+
195
207
/**
196
208
* Returns the target {@link TypeDescriptor} for the given {@link PathInformation} by either inspecting the field or
197
209
* property (the latter preferred) to pick up annotations potentially defined for formatting purposes.
@@ -213,21 +225,21 @@ private static TypeDescriptor getTargetTypeDescriptor(PathInformation path) {
213
225
.nested (new Property (owningType , descriptor .getReadMethod (), descriptor .getWriteMethod (), leafProperty ), 0 );
214
226
215
227
if (result == null ) {
216
- throw new IllegalStateException (String .format ("Could not obtain TypeDesciptor for PathInformation %s!" , path ));
228
+ throw new IllegalStateException (String .format ("Could not obtain TypeDescriptor for PathInformation %s!" , path ));
217
229
}
218
230
219
231
return result ;
220
232
}
221
233
222
234
/**
223
- * Returns whether the given collection has exactly one element that doesn't contain any text. This is basically an
224
- * indicator that a request parameter has been submitted but no value for it.
235
+ * Returns whether the given collection has exactly one element that is empty (i.e. doesn't contain text) . This is
236
+ * basically an indicator that a request parameter has been submitted but no value for it.
225
237
*
226
238
* @param source must not be {@literal null}.
227
239
* @return
228
240
*/
229
- private static boolean isSingleElementCollectionWithoutText (List <String > source ) {
230
- return source .size () == 1 && ! StringUtils . hasLength (source .get (0 ));
241
+ private static boolean isSingleElementCollectionWithEmptyItem (List <? > source ) {
242
+ return source .size () == 1 && ObjectUtils . isEmpty (source .get (0 ));
231
243
}
232
244
233
245
/**
0 commit comments