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