4949
5050import com .fasterxml .jackson .annotation .JsonView ;
5151import io .swagger .v3 .core .util .PrimitiveType ;
52+ import io .swagger .v3 .oas .annotations .enums .Explode ;
5253import 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 ;
5358import io .swagger .v3 .oas .models .Components ;
5459import io .swagger .v3 .oas .models .OpenAPI ;
5560import io .swagger .v3 .oas .models .Operation ;
@@ -237,10 +242,13 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
237242 String [] reflectionParametersNames = Arrays .stream (handlerMethod .getMethod ().getParameters ()).map (java .lang .reflect .Parameter ::getName ).toArray (String []::new );
238243 if (pNames == null || Arrays .stream (pNames ).anyMatch (Objects ::isNull ))
239244 pNames = reflectionParametersNames ;
240- parameters = DelegatingMethodParameter .customize (pNames , parameters , parameterBuilder .getDelegatingMethodParameterCustomizer ());
245+ parameters = DelegatingMethodParameter .customize (pNames , parameters ,
246+ parameterBuilder .getDelegatingMethodParameterCustomizer (),requestMethod );
241247 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 ());
244252 Components components = openAPI .getComponents ();
245253
246254 JavadocProvider javadocProvider = operationService .getJavadocProvider ();
@@ -255,19 +263,32 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
255263 final String pName = methodParameter .getParameterName ();
256264 ParameterInfo parameterInfo = new ParameterInfo (pName , methodParameter , parameterBuilder );
257265
258- if (parameterDoc == null )
266+ if (parameterDoc == null ) {
259267 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
260278 // use documentation as reference
261279 if (parameterDoc != null ) {
262- if (parameterDoc .hidden () || parameterDoc .schema ().hidden ())
280+ if (parameterDoc .hidden () || parameterDoc .schema ().hidden ()) {
263281 continue ;
282+ }
264283
265- parameter = parameterBuilder .buildParameterFromDoc (parameterDoc , components , methodAttributes .getJsonViewAnnotation (), methodAttributes .getLocale ());
284+ parameter = parameterBuilder .buildParameterFromDoc (parameterDoc , components ,
285+ methodAttributes .getJsonViewAnnotation (), methodAttributes .getLocale ());
266286 parameterInfo .setParameterModel (parameter );
267287 }
268288
269289 if (!isParamToIgnore (methodParameter )) {
270- parameter = buildParams (parameterInfo , components , requestMethod , methodAttributes .getJsonViewAnnotation ());
290+ parameter = buildParams (parameterInfo , components , requestMethod ,
291+ methodAttributes .getJsonViewAnnotation ());
271292 // Merge with the operation parameters
272293 parameter = GenericParameterService .mergeParameter (operationParameters , parameter );
273294 List <Annotation > parameterAnnotations = Arrays .asList (methodParameter .getParameterAnnotations ());
@@ -282,24 +303,45 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
282303 applyBeanValidatorAnnotations (parameter , parameterAnnotations );
283304 }
284305 else if (!RequestMethod .GET .equals (requestMethod )) {
285- if (operation .getRequestBody () != null )
306+ if (operation .getRequestBody () != null ) {
286307 requestBodyInfo .setRequestBody (operation .getRequestBody ());
287- requestBodyService .calculateRequestBodyInfo (components , methodAttributes ,
288- parameterInfo , requestBodyInfo );
308+ }
309+ requestBodyService .calculateRequestBodyInfo (components , methodAttributes , parameterInfo ,
310+ requestBodyInfo );
289311 // Add requestBody javadoc
290- if (StringUtils .isBlank (requestBodyInfo .getRequestBody ().getDescription ()) && javadocProvider != null ) {
312+ if (StringUtils .isBlank (requestBodyInfo .getRequestBody ().getDescription ())
313+ && javadocProvider != null ) {
291314 String paramJavadocDescription = getParamJavadoc (javadocProvider , methodParameter , pName );
292315 if (!StringUtils .isBlank (paramJavadocDescription )) {
293316 requestBodyInfo .getRequestBody ().setDescription (paramJavadocDescription );
294317 }
295318 }
296- applyBeanValidatorAnnotations (requestBodyInfo .getRequestBody (), parameterAnnotations , methodParameter .isOptional ());
319+ applyBeanValidatorAnnotations (requestBodyInfo .getRequestBody (), parameterAnnotations ,
320+ methodParameter .isOptional ());
297321 }
298322 customiseParameter (parameter , parameterInfo , operationParameters );
299323 }
300324 }
301325
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
303345 setParams (operation , new ArrayList <>(map .values ()), requestBodyInfo );
304346 return operation ;
305347 }
@@ -707,4 +749,98 @@ private String getParamJavadoc(JavadocProvider javadocProvider, MethodParameter
707749 return paramJavadocDescription ;
708750 }
709751
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+ }
710846}
0 commit comments