OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.58k stars 6.52k forks source link

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

Open Miroku87 opened 5 years ago

Miroku87 commented 5 years ago

Bug Report Checklist

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.

auto-labeler[bot] commented 5 years ago

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.