OpenAPITools / OpenAPITools/openapi-generator
[BUG][Go][Client] form-data in components > requestBodies generated as postBody
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
When generating a Go client that has a multipart/form-data requestBody state under components/requestBodies the generator puts the object described in the schema as postBody in the request, fails to parse it and returns the following error while trying to call the generated function:
Invalid body type multipart/form-data
This happens both if the parameters of the form-data requestBody are required or not.
This does not happen when the multipart/form-data requestBody is stated under the paths: property.
Actual Output
func (a *UserApiService) CreateUsersWithArrayInput(ctx context.Context, userPayload UserPayload) (*http.Response, error) {
[...]
// body params
localVarPostBody = &userPayload
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFormFileName, localVarFileName, localVarFileBytes)
if err != nil {
return nil, err
}
[...]
}
Expected Output (optional parameters example)
type CreateUsersWithArrayInputOpts struct {
Foo optional.String
Bar optional.Interface
}
func (a *UerApiService) CreateUsersWithArrayInput(ctx context.Context, localVarOptionals *CreateUsersWithArrayInputOpts) (*http.Response, error) {
[...]
if localVarOptionals != nil && localVarOptionals.Foo.IsSet() {
localVarFormParams.Add("foo", parameterToString(localVarOptionals.Foo.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.Bar.IsSet() {
localVarFormParams.Add("bar", parameterToString(localVarOptionals.Bar.Value(), "csv"))
}
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFormFileName, localVarFileName, localVarFileBytes)
if err != nil {
return nil, err
}
[...]
}
openapi-generator version
4.0.1-SNAPSHOT
OpenAPI declaration file content or url
openapi: 3.0.0
servers:
- url: http://petstore.swagger.io/v2
info:
description: 'This is a sample server Petstore server. You can find out more about
Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For
this sample, you can use the api key `special-key` to test the authorization filters.'
version: 1.0.0
title: Swagger Petstore
paths:
"/user/createWithArray":
post:
tags:
- user
summary: Creates list of users with given input array
description: ''
operationId: createUsersWithArrayInput
responses:
default:
description: successful operation
requestBody:
"$ref": "#/components/requestBodies/UserArray"
components:
schemas:
User:
type: object
properties:
id:
type: integer
format: int64
username:
type: string
UserPayload:
type: object
properties:
foo:
type: string
bar:
type: array
items:
"$ref": "#/components/schemas/User"
requestBodies:
UserArray:
content:
multipart/form-data:
schema:
"$ref": "#/components/schemas/UserPayload"
description: List of user object
required: true
Command line used for generation
I'm using the docker generator:
sudo docker run --rm -v \
${PWD}:/local \
openapitools/openapi-generator-cli generate \
--package-name foo \
-i /local/openapi.yaml \
-g go \
-o /local
Steps to reproduce
Use the above command to generate a client passing the above YAML as input.
Related issues/PRs
#3043
Suggest a fix
This case should be treated exactly as if the multipart/form-data requestBody was stated under the paths: property. So the data should be inserted in the localVarFormParams variable and not in postBody.
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 by reproducing the issue with the provided OpenAPI YAML and Docker generation command, then trace how the Go generator handles multipart/form-data requestBodies referenced from components versus paths. Done means the generated client places form fields in localVarFormParams rather than postBody and the request no longer returns the invalid body type error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- docker, go, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 50/100