OpenAPITools / OpenAPITools/openapi-generator
[BUG][JAVA] Optional(default) requestBody produces Spring REST parameter annotated with RequestBody(required = true)
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- What's the version of OpenAPI Generator used?
- Have you search for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Bounty to sponsor the fix (example)
Description
If I am not mistaken, request bodies should be optional by default (https://swagger.io/docs/specification/describing-request-body/). Spring / Java code generator creates endpoint that has plain @RequestBody annotation, which means by default required = true and behaves accordingly.
With the generated endpoint, attempting to not provide request body causes HttpMessageNotReadableException:
org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public org.springframework.http.ResponseEntity<java.lang.Void> org.my.restapi.openapi.FooApiController.fooIdBarPost(java.lang.String,org.my.restapi.openapi.model.InlineObject11)
Also posted at https://stackoverflow.com/questions/56524143/should-i-be-able-to-define-a-post-resource-with-optional-message-body
openapi-generator version
Tested with 3.3.4 and 4.0.1 versions of https://mvnrepository.com/artifact/org.openapitools/openapi-generator-gradle-plugin
OpenAPI declaration file content or url
(excerpt)
/foo/{id}/bar:
post: # ok
parameters:
- in: path
name: id
schema:
type: string
requestBody:
required: false
content:
application/json:
schema:
type: object
properties:
bazuu:
type: string
responses:
'200':
description: ok
Command line used for generation
used gradle plugin
Steps to reproduce
created classes with build.gradle
openApiGenerate {
generatorName = "spring"
inputSpec = "$projectDir/src/main/resources/openapi-definition.yaml".toString()
outputDir = "$buildDir/generated-sources".toString()
apiPackage = "org.my.restapi.openapi"
modelPackage = "org.my.restapi.openapi.model"
systemProperties = [
modelDocs: "false",
apis: "",
models: ""
]
configOptions = [
interfaceOnly: "true"
]
}
Related issues/PRs
Suggest a fix
Optional requestBody should produce parameter annotated with 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 with the Spring generator configuration in build.gradle and reproduce the issue with the provided OpenAPI YAML. Inspect the generated FooApiController endpoint and the Spring request-body generation path. Done means an optional requestBody generates the corresponding optional Spring annotation and accepts a request without a body.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi, spring
- Domain
- api, backend, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100