@@ -618,16 +618,20 @@ public void applyBeanValidatorAnnotations(final Parameter parameter, final List<
618
618
*/
619
619
public void applyBeanValidatorAnnotations (final RequestBody requestBody , final List <Annotation > annotations , boolean isOptional ) {
620
620
Map <String , Annotation > annos = new HashMap <>();
621
- boolean requestBodyRequired = false ;
621
+ boolean springRequestBodyRequired = false ;
622
+ boolean swaggerRequestBodyRequired = false ;
622
623
if (!CollectionUtils .isEmpty (annotations )) {
623
624
annotations .forEach (annotation -> annos .put (annotation .annotationType ().getSimpleName (), annotation ));
624
- requestBodyRequired = annotations .stream ()
625
+ springRequestBodyRequired = annotations .stream ()
625
626
.filter (annotation -> org .springframework .web .bind .annotation .RequestBody .class .equals (annotation .annotationType ()))
626
627
.anyMatch (annotation -> ((org .springframework .web .bind .annotation .RequestBody ) annotation ).required ());
628
+ swaggerRequestBodyRequired = annotations .stream ()
629
+ .filter (annotation -> io .swagger .v3 .oas .annotations .parameters .RequestBody .class .equals (annotation .annotationType ()))
630
+ .anyMatch (annotation -> ((io .swagger .v3 .oas .annotations .parameters .RequestBody ) annotation ).required ());
627
631
}
628
632
boolean validationExists = Arrays .stream (ANNOTATIONS_FOR_REQUIRED ).anyMatch (annos ::containsKey );
629
633
630
- if (validationExists || (!isOptional && requestBodyRequired ))
634
+ if (validationExists || (!isOptional && ( springRequestBodyRequired || swaggerRequestBodyRequired ) ))
631
635
requestBody .setRequired (true );
632
636
Content content = requestBody .getContent ();
633
637
for (MediaType mediaType : content .values ()) {
@@ -766,14 +770,25 @@ private boolean isRequestBodyParam(RequestMethod requestMethod, ParameterInfo pa
766
770
767
771
return (isBodyAllowed && (parameterInfo .getParameterModel () == null || parameterInfo .getParameterModel ().getIn () == null ) && !delegatingMethodParameter .isParameterObject ())
768
772
&&
769
- ((methodParameter .getParameterAnnotation (io .swagger .v3 .oas .annotations .parameters .RequestBody .class ) != null
770
- || methodParameter .getParameterAnnotation (org .springframework .web .bind .annotation .RequestBody .class ) != null
771
- || AnnotatedElementUtils .findMergedAnnotation (Objects .requireNonNull (methodParameter .getMethod ()), io .swagger .v3 .oas .annotations .parameters .RequestBody .class ) != null )
773
+ (checkRequestBodyAnnotation (methodParameter )
772
774
|| checkOperationRequestBody (methodParameter )
773
775
|| checkFile (methodParameter )
774
776
|| Arrays .asList (methodAttributes .getMethodConsumes ()).contains (MULTIPART_FORM_DATA_VALUE ));
775
777
}
776
778
779
+ /**
780
+ * Checks whether Swagger's or Spring's RequestBody annotation is present on a parameter or method
781
+ *
782
+ * @param methodParameter the method parameter
783
+ * @return the boolean
784
+ */
785
+ private boolean checkRequestBodyAnnotation (MethodParameter methodParameter ) {
786
+ return methodParameter .hasParameterAnnotation (org .springframework .web .bind .annotation .RequestBody .class )
787
+ || methodParameter .hasParameterAnnotation (io .swagger .v3 .oas .annotations .parameters .RequestBody .class )
788
+ || AnnotatedElementUtils .isAnnotated (Objects .requireNonNull (methodParameter .getParameter ()), io .swagger .v3 .oas .annotations .parameters .RequestBody .class )
789
+ || AnnotatedElementUtils .isAnnotated (Objects .requireNonNull (methodParameter .getMethod ()), io .swagger .v3 .oas .annotations .parameters .RequestBody .class );
790
+ }
791
+
777
792
/**
778
793
* Check file boolean.
779
794
*
0 commit comments