61
61
import jakarta .validation .constraints .Min ;
62
62
import jakarta .validation .constraints .Pattern ;
63
63
import jakarta .validation .constraints .Size ;
64
- import org .apache .commons .lang3 .ArrayUtils ;
65
64
import org .apache .commons .lang3 .StringUtils ;
66
65
import org .springdoc .core .customizers .ParameterCustomizer ;
67
66
import org .springdoc .core .discoverer .SpringDocParameterNameDiscoverer ;
77
76
import org .springframework .core .MethodParameter ;
78
77
import org .springframework .core .annotation .AnnotatedElementUtils ;
79
78
import org .springframework .http .HttpMethod ;
80
- import org .springframework .util .ClassUtils ;
81
79
import org .springframework .util .CollectionUtils ;
82
80
import org .springframework .validation .BindingResult ;
83
81
import org .springframework .validation .Errors ;
93
91
import org .springframework .web .util .UriComponentsBuilder ;
94
92
95
93
import static org .springdoc .core .converters .SchemaPropertyDeprecatingConverter .containsDeprecatedAnnotation ;
94
+ import static org .springdoc .core .service .GenericParameterService .isFile ;
96
95
import static org .springdoc .core .utils .Constants .OPENAPI_ARRAY_TYPE ;
97
96
import static org .springdoc .core .utils .Constants .OPENAPI_STRING_TYPE ;
98
97
99
98
/**
100
99
* The type Abstract request builder.
100
+ *
101
101
* @author bnasslahsen
102
102
*/
103
103
public abstract class AbstractRequestService {
@@ -183,10 +183,10 @@ public abstract class AbstractRequestService {
183
183
/**
184
184
* Instantiates a new Abstract request builder.
185
185
*
186
- * @param parameterBuilder the parameter builder
187
- * @param requestBodyService the request body builder
188
- * @param operationService the operation builder
189
- * @param parameterCustomizers the parameter customizers
186
+ * @param parameterBuilder the parameter builder
187
+ * @param requestBodyService the request body builder
188
+ * @param operationService the operation builder
189
+ * @param parameterCustomizers the parameter customizers
190
190
* @param localSpringDocParameterNameDiscoverer the local spring doc parameter name discoverer
191
191
*/
192
192
protected AbstractRequestService (GenericParameterService parameterBuilder , RequestBodyService requestBodyService ,
@@ -237,7 +237,7 @@ public static boolean isRequestTypeToIgnore(Class<?> rawClass) {
237
237
* Gets headers.
238
238
*
239
239
* @param methodAttributes the method attributes
240
- * @param map the map
240
+ * @param map the map
241
241
* @return the headers
242
242
*/
243
243
@ SuppressWarnings ("unchecked" )
@@ -265,11 +265,11 @@ public static Collection<Parameter> getHeaders(MethodAttributes methodAttributes
265
265
/**
266
266
* Build operation.
267
267
*
268
- * @param handlerMethod the handler method
269
- * @param requestMethod the request method
270
- * @param operation the operation
268
+ * @param handlerMethod the handler method
269
+ * @param requestMethod the request method
270
+ * @param operation the operation
271
271
* @param methodAttributes the method attributes
272
- * @param openAPI the open api
272
+ * @param openAPI the open api
273
273
* @return the operation
274
274
*/
275
275
public Operation build (HandlerMethod handlerMethod , RequestMethod requestMethod ,
@@ -378,10 +378,10 @@ else if (!RequestMethod.GET.equals(requestMethod) || OpenApiVersion.OPENAPI_3_1.
378
378
/**
379
379
* Gets parameter linked hash map.
380
380
*
381
- * @param components the components
382
- * @param methodAttributes the method attributes
381
+ * @param components the components
382
+ * @param methodAttributes the method attributes
383
383
* @param operationParameters the operation parameters
384
- * @param parametersDocMap the parameters doc map
384
+ * @param parametersDocMap the parameters doc map
385
385
* @return the parameter linked hash map
386
386
*/
387
387
private LinkedHashMap <ParameterId , Parameter > getParameterLinkedHashMap (Components components , MethodAttributes methodAttributes , List <Parameter > operationParameters , Map <ParameterId , io .swagger .v3 .oas .annotations .Parameter > parametersDocMap ) {
@@ -425,8 +425,8 @@ private LinkedHashMap<ParameterId, Parameter> getParameterLinkedHashMap(Componen
425
425
/**
426
426
* Customise parameter parameter.
427
427
*
428
- * @param parameter the parameter
429
- * @param parameterInfo the parameter info
428
+ * @param parameter the parameter
429
+ * @param parameterInfo the parameter info
430
430
* @param operationParameters the operation parameters
431
431
*/
432
432
protected void customiseParameter (Parameter parameter , ParameterInfo parameterInfo , List <Parameter > operationParameters ) {
@@ -472,9 +472,9 @@ private boolean isRequiredAnnotation(MethodParameter parameter) {
472
472
/**
473
473
* Sets params.
474
474
*
475
- * @param operation the operation
475
+ * @param operation the operation
476
476
* @param operationParameters the operation parameters
477
- * @param requestBodyInfo the request body info
477
+ * @param requestBodyInfo the request body info
478
478
*/
479
479
private void setParams (Operation operation , List <Parameter > operationParameters , RequestBodyInfo requestBodyInfo ) {
480
480
if (!CollectionUtils .isEmpty (operationParameters ))
@@ -496,10 +496,10 @@ public boolean isValidParameter(Parameter parameter) {
496
496
/**
497
497
* Build params parameter.
498
498
*
499
- * @param parameterInfo the parameter info
500
- * @param components the components
501
- * @param requestMethod the request method
502
- * @param jsonView the json view
499
+ * @param parameterInfo the parameter info
500
+ * @param components the components
501
+ * @param requestMethod the request method
502
+ * @param jsonView the json view
503
503
* @param openApiVersion the open api version
504
504
* @return the parameter
505
505
*/
@@ -526,8 +526,8 @@ public Parameter buildParams(ParameterInfo parameterInfo, Components components,
526
526
* Build param parameter.
527
527
*
528
528
* @param parameterInfo the parameter info
529
- * @param components the components
530
- * @param jsonView the json view
529
+ * @param components the components
530
+ * @param jsonView the json view
531
531
* @return the parameter
532
532
*/
533
533
public Parameter buildParam (ParameterInfo parameterInfo , Components components , JsonView jsonView ) {
@@ -573,7 +573,7 @@ public Parameter buildParam(ParameterInfo parameterInfo, Components components,
573
573
/**
574
574
* Apply bean validator annotations.
575
575
*
576
- * @param parameter the parameter
576
+ * @param parameter the parameter
577
577
* @param annotations the annotations
578
578
*/
579
579
public void applyBeanValidatorAnnotations (final Parameter parameter , final List <Annotation > annotations ) {
@@ -592,7 +592,7 @@ public void applyBeanValidatorAnnotations(final Parameter parameter, final List<
592
592
*
593
593
* @param requestBody the request body
594
594
* @param annotations the annotations
595
- * @param isOptional the is optional
595
+ * @param isOptional the is optional
596
596
*/
597
597
public void applyBeanValidatorAnnotations (final RequestBody requestBody , final List <Annotation > annotations , boolean isOptional ) {
598
598
Map <String , Annotation > annos = new HashMap <>();
@@ -614,10 +614,28 @@ public void applyBeanValidatorAnnotations(final RequestBody requestBody, final L
614
614
}
615
615
}
616
616
617
+ /**
618
+ * Gets request body builder.
619
+ *
620
+ * @return the request body builder
621
+ */
622
+ public RequestBodyService getRequestBodyBuilder () {
623
+ return requestBodyService ;
624
+ }
625
+
626
+ /**
627
+ * Is default flat param object boolean.
628
+ *
629
+ * @return the boolean
630
+ */
631
+ public boolean isDefaultFlatParamObject () {
632
+ return defaultFlatParamObject ;
633
+ }
634
+
617
635
/**
618
636
* Calculate size.
619
637
*
620
- * @param annos the annos
638
+ * @param annos the annos
621
639
* @param schema the schema
622
640
*/
623
641
private void calculateSize (Map <String , Annotation > annos , Schema <?> schema ) {
@@ -634,15 +652,6 @@ else if (OPENAPI_STRING_TYPE.equals(schema.getType())) {
634
652
}
635
653
}
636
654
637
- /**
638
- * Gets request body builder.
639
- *
640
- * @return the request body builder
641
- */
642
- public RequestBodyService getRequestBodyBuilder () {
643
- return requestBodyService ;
644
- }
645
-
646
655
/**
647
656
* Gets api parameters.
648
657
*
@@ -673,7 +682,7 @@ private Map<ParameterId, io.swagger.v3.oas.annotations.Parameter> getApiParamete
673
682
/**
674
683
* Apply validations to schema.
675
684
*
676
- * @param annos the annos
685
+ * @param annos the annos
677
686
* @param schema the schema
678
687
*/
679
688
private void applyValidationsToSchema (Map <String , Annotation > annos , Schema <?> schema ) {
@@ -713,31 +722,65 @@ private void applyValidationsToSchema(Map<String, Annotation> annos, Schema<?> s
713
722
/**
714
723
* Is RequestBody param boolean.
715
724
*
716
- * @param requestMethod the request method
717
- * @param parameterInfo the parameter info
725
+ * @param requestMethod the request method
726
+ * @param parameterInfo the parameter info
718
727
* @param openApiVersion the open api version
719
728
* @return the boolean
720
729
*/
721
730
private boolean isRequestBodyParam (RequestMethod requestMethod , ParameterInfo parameterInfo , String openApiVersion ) {
722
731
MethodParameter methodParameter = parameterInfo .getMethodParameter ();
723
732
DelegatingMethodParameter delegatingMethodParameter = (DelegatingMethodParameter ) methodParameter ;
724
- Boolean isBodyAllowed = !RequestMethod .GET .equals (requestMethod ) || OpenApiVersion .OPENAPI_3_1 .getVersion ().equals (openApiVersion );
733
+ boolean isBodyAllowed = !RequestMethod .GET .equals (requestMethod ) || OpenApiVersion .OPENAPI_3_1 .getVersion ().equals (openApiVersion );
725
734
726
735
return (isBodyAllowed && (parameterInfo .getParameterModel () == null || parameterInfo .getParameterModel ().getIn () == null ) && !delegatingMethodParameter .isParameterObject ())
727
736
&&
728
737
((methodParameter .getParameterAnnotation (io .swagger .v3 .oas .annotations .parameters .RequestBody .class ) != null
729
738
|| methodParameter .getParameterAnnotation (org .springframework .web .bind .annotation .RequestBody .class ) != null
730
- || methodParameter .getParameterAnnotation (org .springframework .web .bind .annotation .RequestPart .class ) != null
731
739
|| AnnotatedElementUtils .findMergedAnnotation (Objects .requireNonNull (methodParameter .getMethod ()), io .swagger .v3 .oas .annotations .parameters .RequestBody .class ) != null )
732
- || (!ClassUtils .isPrimitiveOrWrapper (methodParameter .getParameterType ()) && (!ArrayUtils .isEmpty (methodParameter .getParameterAnnotations ()))));
740
+ || checkOperationRequestBody (methodParameter )
741
+ || checkFile (methodParameter )
742
+
743
+ );
733
744
}
734
745
735
746
/**
736
- * Is default flat param object boolean.
747
+ * Check file boolean.
737
748
*
749
+ * @param methodParameter the method parameter
738
750
* @return the boolean
739
751
*/
740
- public boolean isDefaultFlatParamObject () {
741
- return defaultFlatParamObject ;
752
+ private boolean checkFile (MethodParameter methodParameter ) {
753
+ if (methodParameter .getParameterAnnotation (org .springframework .web .bind .annotation .RequestPart .class ) != null )
754
+ return true ;
755
+ else if (methodParameter .getParameterAnnotation (org .springframework .web .bind .annotation .RequestParam .class ) != null ) {
756
+ return isFile (methodParameter .getParameterType ());
757
+ }
758
+ return false ;
759
+ }
760
+
761
+ /**
762
+ * Check operation request body boolean.
763
+ *
764
+ * @param methodParameter the method parameter
765
+ * @return the boolean
766
+ */
767
+ private boolean checkOperationRequestBody (MethodParameter methodParameter ) {
768
+ if (AnnotatedElementUtils .findMergedAnnotation (Objects .requireNonNull (methodParameter .getMethod ()), io .swagger .v3 .oas .annotations .Operation .class ) != null ) {
769
+ io .swagger .v3 .oas .annotations .Operation operation = AnnotatedElementUtils .findMergedAnnotation (Objects .requireNonNull (methodParameter .getMethod ()), io .swagger .v3 .oas .annotations .Operation .class );
770
+ io .swagger .v3 .oas .annotations .parameters .RequestBody requestBody = operation .requestBody ();
771
+ if (StringUtils .isNotBlank (requestBody .description ()))
772
+ return true ;
773
+ else if (StringUtils .isNotBlank (requestBody .ref ()))
774
+ return true ;
775
+ else if (requestBody .required ())
776
+ return true ;
777
+ else if (requestBody .useParameterTypeSchema ())
778
+ return true ;
779
+ else if (requestBody .content ().length > 0 )
780
+ return true ;
781
+ else
782
+ return requestBody .extensions ().length > 0 ;
783
+ }
784
+ return false ;
742
785
}
743
786
}
0 commit comments