31
31
import java .util .Collection ;
32
32
import java .util .Collections ;
33
33
import java .util .HashMap ;
34
+ import java .util .Iterator ;
34
35
import java .util .LinkedHashMap ;
35
36
import java .util .List ;
36
37
import java .util .Map ;
38
+ import java .util .Map .Entry ;
37
39
import java .util .Objects ;
38
40
import java .util .Optional ;
39
41
import java .util .Set ;
49
51
50
52
import com .fasterxml .jackson .annotation .JsonView ;
51
53
import io .swagger .v3 .core .util .PrimitiveType ;
52
- import io .swagger .v3 .oas .annotations .enums .Explode ;
53
54
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 ;
58
55
import io .swagger .v3 .oas .models .Components ;
59
56
import io .swagger .v3 .oas .models .OpenAPI ;
60
57
import io .swagger .v3 .oas .models .Operation ;
@@ -242,13 +239,10 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
242
239
String [] reflectionParametersNames = Arrays .stream (handlerMethod .getMethod ().getParameters ()).map (java .lang .reflect .Parameter ::getName ).toArray (String []::new );
243
240
if (pNames == null || Arrays .stream (pNames ).anyMatch (Objects ::isNull ))
244
241
pNames = reflectionParametersNames ;
245
- parameters = DelegatingMethodParameter .customize (pNames , parameters ,
246
- parameterBuilder .getDelegatingMethodParameterCustomizer (),requestMethod );
242
+ parameters = DelegatingMethodParameter .customize (pNames , parameters , parameterBuilder .getDelegatingMethodParameterCustomizer ());
247
243
RequestBodyInfo requestBodyInfo = new RequestBodyInfo ();
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
+ List <Parameter > operationParameters = (operation .getParameters () != null ) ? operation .getParameters () : new ArrayList <>();
245
+ Map <String , io .swagger .v3 .oas .annotations .Parameter > parametersDocMap = getApiParameters (handlerMethod .getMethod ());
252
246
Components components = openAPI .getComponents ();
253
247
254
248
JavadocProvider javadocProvider = operationService .getJavadocProvider ();
@@ -263,32 +257,28 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
263
257
final String pName = methodParameter .getParameterName ();
264
258
ParameterInfo parameterInfo = new ParameterInfo (pName , methodParameter , parameterBuilder );
265
259
266
- if (parameterDoc == null ) {
260
+ if (parameterDoc == null )
267
261
parameterDoc = parametersDocMap .get (parameterInfo .getpName ());
268
- }
269
- // TODO Use Schema
262
+
270
263
if (parameterDoc == null ) {
271
264
io .swagger .v3 .oas .annotations .media .Schema schema = AnnotatedElementUtils .findMergedAnnotation (
272
265
AnnotatedElementUtils .forAnnotations (methodParameter .getParameterAnnotations ()), io .swagger .v3 .oas .annotations .media .Schema .class );
273
266
if (schema != null ) {
274
- parameterDoc = generateParameterBySchema (schema );
267
+ parameterDoc = parameterBuilder . generateParameterBySchema (schema );
275
268
}
276
269
}
277
- // TODO Use Schema End
270
+
278
271
// use documentation as reference
279
272
if (parameterDoc != null ) {
280
- if (parameterDoc .hidden () || parameterDoc .schema ().hidden ()) {
273
+ if (parameterDoc .hidden () || parameterDoc .schema ().hidden ())
281
274
continue ;
282
- }
283
275
284
- parameter = parameterBuilder .buildParameterFromDoc (parameterDoc , components ,
285
- methodAttributes .getJsonViewAnnotation (), methodAttributes .getLocale ());
276
+ parameter = parameterBuilder .buildParameterFromDoc (parameterDoc , components , methodAttributes .getJsonViewAnnotation (), methodAttributes .getLocale ());
286
277
parameterInfo .setParameterModel (parameter );
287
278
}
288
279
289
280
if (!isParamToIgnore (methodParameter )) {
290
- parameter = buildParams (parameterInfo , components , requestMethod ,
291
- methodAttributes .getJsonViewAnnotation ());
281
+ parameter = buildParams (parameterInfo , components , requestMethod , methodAttributes .getJsonViewAnnotation ());
292
282
// Merge with the operation parameters
293
283
parameter = GenericParameterService .mergeParameter (operationParameters , parameter );
294
284
List <Annotation > parameterAnnotations = Arrays .asList (methodParameter .getParameterAnnotations ());
@@ -303,45 +293,46 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
303
293
applyBeanValidatorAnnotations (parameter , parameterAnnotations );
304
294
}
305
295
else if (!RequestMethod .GET .equals (requestMethod )) {
306
- if (operation .getRequestBody () != null ) {
296
+ if (operation .getRequestBody () != null )
307
297
requestBodyInfo .setRequestBody (operation .getRequestBody ());
308
- }
309
- requestBodyService .calculateRequestBodyInfo (components , methodAttributes , parameterInfo ,
310
- requestBodyInfo );
298
+ requestBodyService .calculateRequestBodyInfo (components , methodAttributes ,
299
+ parameterInfo , requestBodyInfo );
311
300
// Add requestBody javadoc
312
- if (StringUtils .isBlank (requestBodyInfo .getRequestBody ().getDescription ())
313
- && javadocProvider != null ) {
301
+ if (StringUtils .isBlank (requestBodyInfo .getRequestBody ().getDescription ()) && javadocProvider != null ) {
314
302
String paramJavadocDescription = getParamJavadoc (javadocProvider , methodParameter , pName );
315
303
if (!StringUtils .isBlank (paramJavadocDescription )) {
316
304
requestBodyInfo .getRequestBody ().setDescription (paramJavadocDescription );
317
305
}
318
306
}
319
- applyBeanValidatorAnnotations (requestBodyInfo .getRequestBody (), parameterAnnotations ,
320
- methodParameter .isOptional ());
307
+ applyBeanValidatorAnnotations (requestBodyInfo .getRequestBody (), parameterAnnotations , methodParameter .isOptional ());
321
308
}
322
309
customiseParameter (parameter , parameterInfo , operationParameters );
323
310
}
324
311
}
325
312
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 );
313
+ LinkedHashMap <String , Parameter > map = getParameterLinkedHashMap (components , methodAttributes , operationParameters , parametersDocMap );
314
+ RequestBody requestBody = requestBodyInfo .getRequestBody ();
315
+ // support form-data
316
+ if (requestBody != null
317
+ && requestBody .getContent () != null
318
+ && requestBody .getContent ().containsKey (org .springframework .http .MediaType .MULTIPART_FORM_DATA_VALUE )) {
319
+ io .swagger .v3 .oas .models .media .Schema <?> mergedSchema = requestBodyInfo .getMergedSchema ();
320
+ Iterator <Entry <String , Parameter >> it = map .entrySet ().iterator ();
321
+ while (it .hasNext ()) {
322
+ Entry <String , Parameter > entry = it .next ();
323
+ Parameter parameter = entry .getValue ();
324
+ if (!ParameterIn .PATH .toString ().equals (parameter .getIn ())) {
325
+ io .swagger .v3 .oas .models .media .Schema <?> itemSchema = new io .swagger .v3 .oas .models .media .Schema () ;
326
+ itemSchema .setName (entry .getKey ());
327
+ itemSchema .setDescription (parameter .getDescription ());
328
+ itemSchema .setDeprecated (parameter .getDeprecated ());
329
+ if (parameter .getExample () != null )
330
+ itemSchema .setExample (parameter .getExample ());
331
+ mergedSchema .addProperty (entry .getKey (), itemSchema );
332
+ it .remove ();
333
+ }
341
334
}
342
- map .clear ();
343
335
}
344
- // TODO support form-data END
345
336
setParams (operation , new ArrayList <>(map .values ()), requestBodyInfo );
346
337
return operation ;
347
338
}
@@ -749,98 +740,4 @@ private String getParamJavadoc(JavadocProvider javadocProvider, MethodParameter
749
740
return paramJavadocDescription ;
750
741
}
751
742
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
- }
846
743
}
0 commit comments