Skip to content

[BUG][Go][Client] form-data in components > requestBodies generated as postBody #3045

Open
@Miroku87

Description

@Miroku87

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions