swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[Spring-Boot server] Request body incorrectly generated as not optional by default

Open jose-hernandez-001 opened this issue 6 years ago • 4 comments

Description

According to the OpenAPI 3.0.X specification the request body go a POST request is optional by default (https://swagger.io/docs/specification/describing-request-body/) but the code being generated for my Spring-Boot application does not mark the request body as optional. E.g. this is what is being generated:

,@ApiParam(value = "A request body containing a filter"  )  @Valid @RequestBody FilterDto body
) {
        return getDelegate().getReviewers(body);

To make the request body optional as per the spec, the Spring annotation should be @RequestBody(required = false)

Please note that the request body is still being generated as mandatory even when you force it in the spec as optional by adding the required: false option of the body definition as shown in the example below.

Swagger-codegen version

3.0.21

Swagger declaration file content or url
  /reviewers/v1:
    post:
      tags:
        - "Reviewers"
      summary: "Return a list of potential reviewers based on given search term and optional filters"
      operationId: "getReviewers"
      requestBody:
        $ref: '#/components/requestBodies/FilterRequestBodyDto'
...
components:
  requestBodies:
    FilterRequestBodyDto:
      description: "A request body containing a filter"
      required: false
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/FilterDto"
Command line used for generation

Generated via the Swagger Codegen maven plugin

Steps to reproduce

Add a response body as shown above to a spec and take a look at the generated API interface file to see the RequestBody annotation has the required parameter explicitly set to false.

Related issues/PRs

None known

Suggest a fix/enhancement

Add the required = false parameter to the annotation that controls whether the body is optional. e.g. @RequestBody(required = false)

jose-hernandez-001 avatar Sep 16 '20 10:09 jose-hernandez-001

Any progress on the issue? Why can't a request body be optional (required: false)?

flo-ferox avatar Sep 28 '21 07:09 flo-ferox

we have this issue with latest maven plugin swagger-codegen-maven-plugin version 3.0.34

sandeepamilineni avatar Jan 06 '23 11:01 sandeepamilineni

Still have the issue in 3.0.42

petros94 avatar May 02 '23 14:05 petros94

Still have the issue in 3.0.46

jose-hernandez-001 avatar Jul 20 '23 09:07 jose-hernandez-001