swagger-api / swagger-api/swagger-codegen
[Spring-Boot server] Request body incorrectly generated as not optional by default
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
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)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the Spring-Boot server generator and trace how the supplied OpenAPI requestBody required value reaches the generated @RequestBody annotation. Reproduce the issue with the YAML example, then verify that omitted or false required values generate an optional request body while explicitly required bodies remain mandatory.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring-boot
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100