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] TypeScript Fetch client doesn't support multipart/form-data body #1905

Open emadelwany opened 5 years ago

emadelwany commented 5 years ago

Bug Report Checklist

Description

Generated TypeScript code doesn't properly handle FormData, the definition below generates:

            if (file !== undefined) { 
                localVarFormParams.set('file', file as any);
            }

            localVarHeaderParameter['Content-Type'] = 'application/x-www-form-urlencoded';

When it should generate

const formData = new FormData();
formData.append("file", file);
localVarFormParams.body = formData;
localVarHeaderParameter['Content-Type'] = 'multiparm/form-data';
openapi-generator version

3.3.4

 "/api/Documents": {
      "post": {
        "tags": [
          "Documents"
        ],
        "operationId": "Upload",
        "consumes": [
          "multipart/form-data"
        ],
        "produces": [

        ],
        "parameters": [
          {
            "name": "file",
            "in": "formData",
            "required": false,
            "type": "file"
          }
        ],
        "responses": {
          "200": {
            "description": "Success"
          }
        }
      }

(for JSON code), so it becomes more readable. If it is longer than about ten lines, please create a Gist (https://gist.github.com) or upload it somewhere else and link it here. -->

Command line used for generation

docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v3.3.4 generate \ -i "http://host.docker.internal/swagger/v1/swagger.json" \ -g typescript-fetch \ --type-mappings Date=string \ -o /local/src/swagger

Steps to reproduce
Related issues/PRs
Suggest a fix
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.

sem4phor commented 4 years ago

Can this be closed? I believe this is fixed since we use formdata bodies and the genereated fetch client code looks like this:

const consumes: runtime.Consume[] = [
            { contentType: 'multipart/form-data' },
        ];
        const canConsumeForm = runtime.canConsumeForm(consumes);
        let formParams: { append(param: string, value: any): any };
        let useForm = false;
        useForm = canConsumeForm;
        if (useForm) {
            formParams = new FormData();
        } else {
            formParams = new URLSearchParams();
        }
        if (requestParameters.file !== undefined) {
            formParams.append('file', requestParameters.file as any);
        }
        if (requestParameters.documentType !== undefined) {
            formParams.append('document_type', requestParameters.documentType as any);
        }
        const response = await this.request({
            path: `/users/{userId}/document`.replace(`{${"userId"}}`, encodeURIComponent(String(requestParameters.userId))),
            method: 'POST',
            headers: headerParameters,
            query: queryParameters,
            body: formParams,
        });