swagger-api / swagger-api/swagger-codegen
[Java spring] Failed to generate multi-part request body
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Hey guys!
[Java spring] When using multipart/form-data in requestBody, codegen fails to generate corresponding parameter in API.
And using application/json works fine.
Swagger-codegen version
3.0.34
Yaml
Using multipart/form-data as content type:
/insight/v1/attachments:download:
post:
tags:
- CustomerProfileAttachmentRestService
description: batch download files
summary: download files
operationId: downloadCustomerProfileDocuments
requestBody:
description: file multi parts
required: true
content:
multipart/form-data:
schema:
type: array
items:
type: string
minLength: 1
responses:
200:
description: Successful response
content:
application/json:
schema:
type: object # just for testing, not a real response
And it failed to generate param.
After debuging, what I found is that in AbstractJavaCodegen , method postProcessOperations, the corresponding operation in its return object "objs" has wrong content, which its contentType is null. And eventually cause failure of generating such param.
And in contract, yaml like below, which just change the content media type to application/json, are manage to generate properly.
/insight/v1/attachments:download:
post:
tags:
- CustomerProfileAttachmentRestService
description: batch download files
summary: download files
operationId: downloadCustomerProfileDocuments
requestBody:
description: file multi parts
required: true
content:
application/json:
schema:
type: array
items:
type: string
minLength: 1
responses:
200:
description: Successful response
content:
application/json:
schema:
type: object # just for testing, not a real response
Debugging shows that the content of CodegenOperation is right, which its contentType is application/json and it contains a parameter. Then it generate param properly.
Suggest a fix/enhancement
At least, developers are manageed to get the content below such media type multipart/form-data, instead of nothing at all in content, and we cannot find these content anywhere else.
And I still cannot figure out why multipart/form-data is not supported, since its allowed by OpenApi 3.0.x.
Thanks!
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 AbstractJavaCodegen.postProcessOperations and the supplied OpenAPI YAML, comparing the multipart/form-data and application/json cases. Inspect how the CodegenOperation contentType and parameters are populated. Done means the multipart request body produces the corresponding generated parameter instead of being omitted.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100