49
49
50
50
import com .fasterxml .jackson .annotation .JsonView ;
51
51
import io .swagger .v3 .core .util .PrimitiveType ;
52
+ import io .swagger .v3 .oas .annotations .enums .Explode ;
52
53
import io .swagger .v3 .oas .annotations .enums .ParameterIn ;
54
+ import io .swagger .v3 .oas .annotations .enums .ParameterStyle ;
55
+ import io .swagger .v3 .oas .annotations .extensions .Extension ;
56
+ import io .swagger .v3 .oas .annotations .media .ArraySchema ;
57
+ import io .swagger .v3 .oas .annotations .media .ExampleObject ;
53
58
import io .swagger .v3 .oas .models .Components ;
54
59
import io .swagger .v3 .oas .models .OpenAPI ;
55
60
import io .swagger .v3 .oas .models .Operation ;
@@ -237,10 +242,13 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
237
242
String [] reflectionParametersNames = Arrays .stream (handlerMethod .getMethod ().getParameters ()).map (java .lang .reflect .Parameter ::getName ).toArray (String []::new );
238
243
if (pNames == null || Arrays .stream (pNames ).anyMatch (Objects ::isNull ))
239
244
pNames = reflectionParametersNames ;
240
- parameters = DelegatingMethodParameter .customize (pNames , parameters , parameterBuilder .getDelegatingMethodParameterCustomizer ());
245
+ parameters = DelegatingMethodParameter .customize (pNames , parameters ,
246
+ parameterBuilder .getDelegatingMethodParameterCustomizer (),requestMethod );
241
247
RequestBodyInfo requestBodyInfo = new RequestBodyInfo ();
242
- List <Parameter > operationParameters = (operation .getParameters () != null ) ? operation .getParameters () : new ArrayList <>();
243
- Map <String , io .swagger .v3 .oas .annotations .Parameter > parametersDocMap = getApiParameters (handlerMethod .getMethod ());
248
+ List <Parameter > operationParameters = (operation .getParameters () != null ) ? operation .getParameters ()
249
+ : new ArrayList <>();
250
+ Map <String , io .swagger .v3 .oas .annotations .Parameter > parametersDocMap = getApiParameters (
251
+ handlerMethod .getMethod ());
244
252
Components components = openAPI .getComponents ();
245
253
246
254
JavadocProvider javadocProvider = operationService .getJavadocProvider ();
@@ -255,19 +263,32 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
255
263
final String pName = methodParameter .getParameterName ();
256
264
ParameterInfo parameterInfo = new ParameterInfo (pName , methodParameter , parameterBuilder );
257
265
258
- if (parameterDoc == null )
266
+ if (parameterDoc == null ) {
259
267
parameterDoc = parametersDocMap .get (parameterInfo .getpName ());
268
+ }
269
+ // TODO Use Schema
270
+ if (parameterDoc == null ) {
271
+ io .swagger .v3 .oas .annotations .media .Schema schema = AnnotatedElementUtils .findMergedAnnotation (
272
+ AnnotatedElementUtils .forAnnotations (methodParameter .getParameterAnnotations ()), io .swagger .v3 .oas .annotations .media .Schema .class );
273
+ if (schema != null ) {
274
+ parameterDoc = generateParameterBySchema (schema );
275
+ }
276
+ }
277
+ // TODO Use Schema End
260
278
// use documentation as reference
261
279
if (parameterDoc != null ) {
262
- if (parameterDoc .hidden () || parameterDoc .schema ().hidden ())
280
+ if (parameterDoc .hidden () || parameterDoc .schema ().hidden ()) {
263
281
continue ;
282
+ }
264
283
265
- parameter = parameterBuilder .buildParameterFromDoc (parameterDoc , components , methodAttributes .getJsonViewAnnotation (), methodAttributes .getLocale ());
284
+ parameter = parameterBuilder .buildParameterFromDoc (parameterDoc , components ,
285
+ methodAttributes .getJsonViewAnnotation (), methodAttributes .getLocale ());
266
286
parameterInfo .setParameterModel (parameter );
267
287
}
268
288
269
289
if (!isParamToIgnore (methodParameter )) {
270
- parameter = buildParams (parameterInfo , components , requestMethod , methodAttributes .getJsonViewAnnotation ());
290
+ parameter = buildParams (parameterInfo , components , requestMethod ,
291
+ methodAttributes .getJsonViewAnnotation ());
271
292
// Merge with the operation parameters
272
293
parameter = GenericParameterService .mergeParameter (operationParameters , parameter );
273
294
List <Annotation > parameterAnnotations = Arrays .asList (methodParameter .getParameterAnnotations ());
@@ -282,24 +303,45 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
282
303
applyBeanValidatorAnnotations (parameter , parameterAnnotations );
283
304
}
284
305
else if (!RequestMethod .GET .equals (requestMethod )) {
285
- if (operation .getRequestBody () != null )
306
+ if (operation .getRequestBody () != null ) {
286
307
requestBodyInfo .setRequestBody (operation .getRequestBody ());
287
- requestBodyService .calculateRequestBodyInfo (components , methodAttributes ,
288
- parameterInfo , requestBodyInfo );
308
+ }
309
+ requestBodyService .calculateRequestBodyInfo (components , methodAttributes , parameterInfo ,
310
+ requestBodyInfo );
289
311
// Add requestBody javadoc
290
- if (StringUtils .isBlank (requestBodyInfo .getRequestBody ().getDescription ()) && javadocProvider != null ) {
312
+ if (StringUtils .isBlank (requestBodyInfo .getRequestBody ().getDescription ())
313
+ && javadocProvider != null ) {
291
314
String paramJavadocDescription = getParamJavadoc (javadocProvider , methodParameter , pName );
292
315
if (!StringUtils .isBlank (paramJavadocDescription )) {
293
316
requestBodyInfo .getRequestBody ().setDescription (paramJavadocDescription );
294
317
}
295
318
}
296
- applyBeanValidatorAnnotations (requestBodyInfo .getRequestBody (), parameterAnnotations , methodParameter .isOptional ());
319
+ applyBeanValidatorAnnotations (requestBodyInfo .getRequestBody (), parameterAnnotations ,
320
+ methodParameter .isOptional ());
297
321
}
298
322
customiseParameter (parameter , parameterInfo , operationParameters );
299
323
}
300
324
}
301
325
302
- LinkedHashMap <String , Parameter > map = getParameterLinkedHashMap (components , methodAttributes , operationParameters , parametersDocMap );
326
+ LinkedHashMap <String , Parameter > map = getParameterLinkedHashMap (components , methodAttributes ,
327
+ operationParameters , parametersDocMap );
328
+ RequestBody body = requestBodyInfo .getRequestBody ();
329
+ // TODO support form-data
330
+ if (body != null && body .getContent () != null && body .getContent ().containsKey ("multipart/form-data" )) {
331
+ Set <String > keys = map .keySet ();
332
+ io .swagger .v3 .oas .models .media .Schema mergedSchema = requestBodyInfo .getMergedSchema ();
333
+ for (String key : keys ) {
334
+ Parameter parameter = map .get (key );
335
+ io .swagger .v3 .oas .models .media .Schema itemSchema = new io .swagger .v3 .oas .models .media .Schema ();
336
+ itemSchema .setName (key );
337
+ itemSchema .setDescription (parameter .getDescription ());
338
+ itemSchema .setDeprecated (parameter .getDeprecated ());
339
+ itemSchema .setExample (parameter .getExample ());
340
+ mergedSchema .addProperty (key , itemSchema );
341
+ }
342
+ map .clear ();
343
+ }
344
+ // TODO support form-data END
303
345
setParams (operation , new ArrayList <>(map .values ()), requestBodyInfo );
304
346
return operation ;
305
347
}
@@ -707,4 +749,98 @@ private String getParamJavadoc(JavadocProvider javadocProvider, MethodParameter
707
749
return paramJavadocDescription ;
708
750
}
709
751
752
+ private io .swagger .v3 .oas .annotations .Parameter generateParameterBySchema (io .swagger .v3 .oas .annotations .media .Schema schema ) {
753
+ return new io .swagger .v3 .oas .annotations .Parameter () {
754
+
755
+ @ Override
756
+ public Class <? extends Annotation > annotationType () {
757
+ return io .swagger .v3 .oas .annotations .Parameter .class ;
758
+ }
759
+
760
+ @ Override
761
+ public String name () {
762
+ return schema .name ();
763
+ }
764
+
765
+ @ Override
766
+ public ParameterIn in () {
767
+ return ParameterIn .DEFAULT ;
768
+ }
769
+
770
+ @ Override
771
+ public String description () {
772
+ return schema .description ();
773
+ }
774
+
775
+ @ Override
776
+ public boolean required () {
777
+ return schema .required ();
778
+ }
779
+
780
+ @ Override
781
+ public boolean deprecated () {
782
+ return schema .deprecated ();
783
+ }
784
+
785
+ @ Override
786
+ public boolean allowEmptyValue () {
787
+ return false ;
788
+ }
789
+
790
+ @ Override
791
+ public ParameterStyle style () {
792
+ return ParameterStyle .DEFAULT ;
793
+ }
794
+
795
+ @ Override
796
+ public Explode explode () {
797
+ return Explode .DEFAULT ;
798
+ }
799
+
800
+ @ Override
801
+ public boolean allowReserved () {
802
+ return false ;
803
+ }
804
+
805
+ @ Override
806
+ public io .swagger .v3 .oas .annotations .media .Schema schema () {
807
+ return schema ;
808
+ }
809
+
810
+ @ Override
811
+ public ArraySchema array () {
812
+ return null ;
813
+ }
814
+
815
+ @ Override
816
+ public io .swagger .v3 .oas .annotations .media .Content [] content () {
817
+ return new io .swagger .v3 .oas .annotations .media .Content [0 ];
818
+ }
819
+
820
+ @ Override
821
+ public boolean hidden () {
822
+ return schema .hidden ();
823
+ }
824
+
825
+ @ Override
826
+ public ExampleObject [] examples () {
827
+ return new ExampleObject [0 ];
828
+ }
829
+
830
+ @ Override
831
+ public String example () {
832
+ return schema .example ();
833
+ }
834
+
835
+ @ Override
836
+ public Extension [] extensions () {
837
+ return schema .extensions ();
838
+ }
839
+
840
+ @ Override
841
+ public String ref () {
842
+ return schema .ref ();
843
+ }
844
+ };
845
+ }
710
846
}
0 commit comments