3131import java .util .Collection ;
3232import java .util .Collections ;
3333import java .util .HashMap ;
34+ import java .util .Iterator ;
3435import java .util .LinkedHashMap ;
3536import java .util .List ;
3637import java .util .Map ;
38+ import java .util .Map .Entry ;
3739import java .util .Objects ;
3840import java .util .Optional ;
3941import java .util .Set ;
4951
5052import com .fasterxml .jackson .annotation .JsonView ;
5153import io .swagger .v3 .core .util .PrimitiveType ;
52- import io .swagger .v3 .oas .annotations .enums .Explode ;
5354import 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 ;
5855import io .swagger .v3 .oas .models .Components ;
5956import io .swagger .v3 .oas .models .OpenAPI ;
6057import io .swagger .v3 .oas .models .Operation ;
@@ -242,13 +239,10 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
242239 String [] reflectionParametersNames = Arrays .stream (handlerMethod .getMethod ().getParameters ()).map (java .lang .reflect .Parameter ::getName ).toArray (String []::new );
243240 if (pNames == null || Arrays .stream (pNames ).anyMatch (Objects ::isNull ))
244241 pNames = reflectionParametersNames ;
245- parameters = DelegatingMethodParameter .customize (pNames , parameters ,
246- parameterBuilder .getDelegatingMethodParameterCustomizer (),requestMethod );
242+ parameters = DelegatingMethodParameter .customize (pNames , parameters , parameterBuilder .getDelegatingMethodParameterCustomizer ());
247243 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 ());
252246 Components components = openAPI .getComponents ();
253247
254248 JavadocProvider javadocProvider = operationService .getJavadocProvider ();
@@ -263,32 +257,28 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
263257 final String pName = methodParameter .getParameterName ();
264258 ParameterInfo parameterInfo = new ParameterInfo (pName , methodParameter , parameterBuilder );
265259
266- if (parameterDoc == null ) {
260+ if (parameterDoc == null )
267261 parameterDoc = parametersDocMap .get (parameterInfo .getpName ());
268- }
269- // TODO Use Schema
262+
270263 if (parameterDoc == null ) {
271264 io .swagger .v3 .oas .annotations .media .Schema schema = AnnotatedElementUtils .findMergedAnnotation (
272265 AnnotatedElementUtils .forAnnotations (methodParameter .getParameterAnnotations ()), io .swagger .v3 .oas .annotations .media .Schema .class );
273266 if (schema != null ) {
274- parameterDoc = generateParameterBySchema (schema );
267+ parameterDoc = parameterBuilder . generateParameterBySchema (schema );
275268 }
276269 }
277- // TODO Use Schema End
270+
278271 // use documentation as reference
279272 if (parameterDoc != null ) {
280- if (parameterDoc .hidden () || parameterDoc .schema ().hidden ()) {
273+ if (parameterDoc .hidden () || parameterDoc .schema ().hidden ())
281274 continue ;
282- }
283275
284- parameter = parameterBuilder .buildParameterFromDoc (parameterDoc , components ,
285- methodAttributes .getJsonViewAnnotation (), methodAttributes .getLocale ());
276+ parameter = parameterBuilder .buildParameterFromDoc (parameterDoc , components , methodAttributes .getJsonViewAnnotation (), methodAttributes .getLocale ());
286277 parameterInfo .setParameterModel (parameter );
287278 }
288279
289280 if (!isParamToIgnore (methodParameter )) {
290- parameter = buildParams (parameterInfo , components , requestMethod ,
291- methodAttributes .getJsonViewAnnotation ());
281+ parameter = buildParams (parameterInfo , components , requestMethod , methodAttributes .getJsonViewAnnotation ());
292282 // Merge with the operation parameters
293283 parameter = GenericParameterService .mergeParameter (operationParameters , parameter );
294284 List <Annotation > parameterAnnotations = Arrays .asList (methodParameter .getParameterAnnotations ());
@@ -303,45 +293,46 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
303293 applyBeanValidatorAnnotations (parameter , parameterAnnotations );
304294 }
305295 else if (!RequestMethod .GET .equals (requestMethod )) {
306- if (operation .getRequestBody () != null ) {
296+ if (operation .getRequestBody () != null )
307297 requestBodyInfo .setRequestBody (operation .getRequestBody ());
308- }
309- requestBodyService .calculateRequestBodyInfo (components , methodAttributes , parameterInfo ,
310- requestBodyInfo );
298+ requestBodyService .calculateRequestBodyInfo (components , methodAttributes ,
299+ parameterInfo , requestBodyInfo );
311300 // Add requestBody javadoc
312- if (StringUtils .isBlank (requestBodyInfo .getRequestBody ().getDescription ())
313- && javadocProvider != null ) {
301+ if (StringUtils .isBlank (requestBodyInfo .getRequestBody ().getDescription ()) && javadocProvider != null ) {
314302 String paramJavadocDescription = getParamJavadoc (javadocProvider , methodParameter , pName );
315303 if (!StringUtils .isBlank (paramJavadocDescription )) {
316304 requestBodyInfo .getRequestBody ().setDescription (paramJavadocDescription );
317305 }
318306 }
319- applyBeanValidatorAnnotations (requestBodyInfo .getRequestBody (), parameterAnnotations ,
320- methodParameter .isOptional ());
307+ applyBeanValidatorAnnotations (requestBodyInfo .getRequestBody (), parameterAnnotations , methodParameter .isOptional ());
321308 }
322309 customiseParameter (parameter , parameterInfo , operationParameters );
323310 }
324311 }
325312
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+ }
341334 }
342- map .clear ();
343335 }
344- // TODO support form-data END
345336 setParams (operation , new ArrayList <>(map .values ()), requestBodyInfo );
346337 return operation ;
347338 }
@@ -749,98 +740,4 @@ private String getParamJavadoc(JavadocProvider javadocProvider, MethodParameter
749740 return paramJavadocDescription ;
750741 }
751742
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- }
846743}
0 commit comments