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.55k stars 6.51k forks source link

[BUG] [typescript-axios] Upload file with multipart misses to add boundary with node #8002

Open eisenreich opened 3 years ago

eisenreich commented 3 years ago

Bug Report Checklist

Description

When using typescript-axios in a node environment, it's not able to upload a image because axios does not automatically add the boundary to the header, ref see here.

openapi-generator version

2.1.6

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: Bug API
  version: 1.0.0
tags:
  - name: image
    description: Upload a image
paths:
  /image:
    put:
      tags:
        - image
      summary: Update current map image
      operationId: setImage
      requestBody:
        content:
          multipart/form-data:
            schema:
              properties:
                file:
                  type: string
                  format: binary
                  description: The file to upload
      responses:
        200:
          description: Image successful uploaded
Generation Details
npx @openapitools/openapi-generator-cli generate -i bug-spec.yaml -g typescript-axios --additional-properties supportsES6=true -o generated
Steps to reproduce

Execute the command above and take a look at ImageApiAxiosParamCreator:54.

localVarHeaderParameter['Content-Type'] = 'multipart/form-data';
Related issues/PRs
Suggest a fix

I already have an idea how it could be fixed, but I have to say that I only have beginner experience, so please check if it makes sense.

if (typeof((localVarFormParams as any).getHeaders) === 'function') {
  const headers = (localVarFormParams as any).getHeaders();
  for (const [key, value] of Object.entries(headers)) {
    localVarHeaderParameter[key] = value;
  }
} else {
  localVarHeaderParameter['Content-Type'] = 'multipart/form-data';
}
auto-labeler[bot] commented 3 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.

p-vogt commented 3 years ago

Any updates? Same for typescript-nest.

clement-buchart commented 1 year ago

As a workaround, you can add an interceptor to the axios instance provided to the api clients

instance.interceptors.request.use((config) => {
  if (config.data && config.data instanceof FormData) {
    return {
      ...config,
      headers: {
        ...config.headers,
        ...config.data.getHeaders()
      }
    }
  }
  return config
})