[Spring]: RequestBody required-handling
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 62/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- java, openapi, spring
- Domain
- backend-api-design
Research direction
The bug is that the spring generator ignores requestBody.required: false and always emits @RequestBody with its default required=true. Start in the Spring server templates and find where the @RequestBody annotation is written for request bodies. Make it pass required from the spec, then check that a POST to /search with no body and no query parameter is accepted. Done when the generated controller compiles and both curl cases from the issue succeed.
Written by the indexing model from the issue text.
Description
Description
My goal is to have an endpoint to enable a quick search and an extended search. Using the quick search the endpoint only needs a query-param. Using the extended search a full search-body is required.
I was defining my openapi as follows:
/search:
post:
requestBody:
required: false
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/SearchCriterion"
parameters:
- in: query
name: value
required: false
schema:
type: string
responses:
"200":
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/SearchResult"
I was expecting to be able to send curl requests like:
- curl -X POST
'http://localhost/search'
-H 'Content-Type: application/json'
-d '[{"type":"NAME","value":"Dummy"}]' - curl -i -X POST
'http://localhost/search?value=something'
-H 'Content-Type: application/json'
But only the first request returned any data. The second request only resulted in a 404.
After shortly analysing the problem I found that the required-property is not handed to the @RequestBody Spring annotation. As the annotation uses true as default for the required attribute, I need to hand the body for every request.
This behaviour should be altered to correctly pass the required-property to the code.
Version
We are using [email protected] (which uses [email protected]) but the current main is also not passing the required-property.
Language / Generator Affected
- spring (i haven't checked any other generators as this should be specific for this generator)
OpenAPI/Swagger Spec
/search:
post:
requestBody:
required: false
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/SearchCriterion"
parameters:
- in: query
name: value
required: false
schema:
type: string
responses:
"200":
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/SearchResult"
Environment
OS: Windows 11
Java Version: temurin-21.0.6+7.0.LTS
Build Tool: Maven
swagger-codegen-generators Version: 1.0.54
Checklist
- [/] I have searched the existing issues to avoid duplicates.
- [/] I have included a minimal reproducible spec or configuration.
- [/] I have clearly described the steps to reproduce the issue.
- [/] I have specified which generator or template is affected.
- Dominant language
- Mustache
- Stars
- 299
- Forks
- 439
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing 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.
More from swagger-api/swagger-codegen-generators
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
[Bug]: Python/pythonFlaskConnexion generators validate map keys instead of values for additionalProperties.enumPossibly taken @pvillard31 claimed this 208 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
Python generated code: Delete API Requests can be blocked in API Gateways when sends empty bodyOpen
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
[Bug]: Bean validation doesn't work for numbers larger than Integer.MAX_VALUEPossibly taken @apupier claimed this 296 days ago. OpenBug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
swagger-api/swagger-codegen-generators#1370 · 3 comments · 1 reaction ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
All issues in swagger-api/swagger-codegen-generators
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
ROCm/rocm-libraries#15665 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
shadps4-emu/shadPS4#5705 ·
Maintainers usually reply within 1 day
-
bug multiple issues priority: high
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
storytold/gridcraft#3928 · 1 comment ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Qiskit/qiskit-aer#2533 ·
Maintainers usually reply within 1 day