24
24
25
25
import java .lang .reflect .Field ;
26
26
import java .lang .reflect .Method ;
27
+ import java .lang .reflect .Type ;
27
28
import java .util .Arrays ;
28
29
29
30
import io .swagger .v3 .oas .annotations .enums .ParameterIn ;
39
40
import org .springdoc .core .SpringDocAnnotationsUtils ;
40
41
41
42
import org .springframework .core .MethodParameter ;
43
+ import org .springframework .core .ResolvableType ;
42
44
import org .springframework .core .annotation .AnnotatedElementUtils ;
43
45
import org .springframework .data .repository .query .Param ;
44
46
import org .springframework .data .rest .core .mapping .MethodResourceMapping ;
54
56
55
57
/**
56
58
* The type Data rest operation builder.
59
+ *
57
60
* @author bnasslahsen
58
61
*/
59
62
public class DataRestOperationService {
@@ -202,17 +205,8 @@ private Operation buildSearchOperation(HandlerMethod handlerMethod, DataRestRepo
202
205
String pName = parameterMetadatum .getName ();
203
206
ResourceDescription description = parameterMetadatum .getDescription ();
204
207
if (description instanceof TypedResourceDescription ) {
205
- TypedResourceDescription typedResourceDescription = (TypedResourceDescription ) description ;
206
- Field fieldType = FieldUtils .getField (TypedResourceDescription .class , "type" , true );
207
- Class <?> type ;
208
- try {
209
- type = (Class <?>) fieldType .get (typedResourceDescription );
210
- }
211
- catch (IllegalAccessException e ) {
212
- LOGGER .warn (e .getMessage ());
213
- type = String .class ;
214
- }
215
- Schema <?> schema = SpringDocAnnotationsUtils .resolveSchemaFromType (type , openAPI .getComponents (), null , null );
208
+ Type type = getParameterType (pName ,method ,description );
209
+ Schema <?> schema = SpringDocAnnotationsUtils .extractSchema (openAPI .getComponents (), type , null , null );
216
210
Parameter parameter = getParameterFromAnnotations (openAPI , methodAttributes , method , pName );
217
211
if (parameter == null )
218
212
parameter = new Parameter ().name (pName ).in (ParameterIn .QUERY .toString ()).schema (schema );
@@ -231,6 +225,39 @@ private Operation buildSearchOperation(HandlerMethod handlerMethod, DataRestRepo
231
225
return operation ;
232
226
}
233
227
228
+ /**
229
+ * Gets parameter type.
230
+ *
231
+ * @param pName the p name
232
+ * @param method the method
233
+ * @param description the description
234
+ * @return the parameter type
235
+ */
236
+ private Type getParameterType (String pName , Method method , ResourceDescription description ) {
237
+ Type type = null ;
238
+ java .lang .reflect .Parameter [] parameters = method .getParameters ();
239
+ for (int i = 0 ; i < parameters .length ; i ++) {
240
+ java .lang .reflect .Parameter parameter = parameters [i ];
241
+ if (pName .equals (parameter .getName ()) || pName .equals (parameter .getAnnotation (Param .class ).value ())) {
242
+ ResolvableType resolvableType = ResolvableType .forMethodParameter (method , i );
243
+ type = resolvableType .getType ();
244
+ break ;
245
+ }
246
+ }
247
+ if (type == null ) {
248
+ TypedResourceDescription typedResourceDescription = (TypedResourceDescription ) description ;
249
+ Field fieldType = FieldUtils .getField (TypedResourceDescription .class , "type" , true );
250
+ try {
251
+ type = (Type ) fieldType .get (typedResourceDescription );
252
+ }
253
+ catch (IllegalAccessException e ) {
254
+ LOGGER .warn (e .getMessage ());
255
+ type = String .class ;
256
+ }
257
+ }
258
+ return type ;
259
+ }
260
+
234
261
/**
235
262
* Update parameter from annotations parameter.
236
263
*
@@ -279,6 +306,7 @@ private Operation initOperation(HandlerMethod handlerMethod, Class<?> domainType
279
306
280
307
/**
281
308
* Add operation description.
309
+ *
282
310
* @param operation the operation
283
311
* @param requestMethod the request method
284
312
* @param entity the entity
0 commit comments