[Spring-Boot server] Request body incorrectly generated as not optional by default
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)
Any progress on the issue?
Why can't a request body be optional (required: false)?
we have this issue with latest maven plugin swagger-codegen-maven-plugin version 3.0.34
Still have the issue in 3.0.42
Still have the issue in 3.0.46