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.48k stars 6.5k forks source link

[BUG] Typescript fetch not sending fetch data as multipart for format base64 or byte #9850

Open tusharchutani opened 3 years ago

tusharchutani commented 3 years ago

Bug Report Checklist

Description

We have an upload endpoint for which the format for the file to upload is of type: byte (look for lensData in the yml below) . When we create a typescript swagger client the data to the endpoint does not get sent as formData.

After digging a little into the code of the client the culprit is this piece of code

const canConsumeForm = runtime.canConsumeForm(consumes);

        let formParams: { append(param: string, value: any): any };
        let useForm = false;
        if (useForm) {
            formParams = new FormData();
        } else {
            formParams = new URLSearchParams();
        }

which gets generated from this mustache

       const consumes: runtime.Consume[] = [
            {{#consumes}}
            { contentType: '{{{mediaType}}}' },
            {{/consumes}}
        ];
        // @ts-ignore: canConsumeForm may be unused
        const canConsumeForm = runtime.canConsumeForm(consumes);

        let formParams: { append(param: string, value: any): any };
        let useForm = false;
        {{#formParams}}
        {{#isFile}}
        // use FormData to transmit files using content-type "multipart/form-data"
        useForm = canConsumeForm;
        {{/isFile}}
        {{/formParams}}
        if (useForm) {
            formParams = new FormData();
        } else {
            formParams = new URLSearchParams();
        }

It seems like the isFile value only gets set to true if and only if we have set the format of the lensData to binary. Has anybody else encountered this before?

openapi-generator version

5.1.1

OpenAPI declaration file content or url
  /load-lens:
    post:
      summary: Load Lenses defined in yaml or json format
      operationId: loadLens
      parameters:
        - name: tenantUUID
          in: header
          description: tenant UUID
          required: true
          schema:
            type: string
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                lensData:
                  type: string
                  format: byte
            encoding:
              lensData:
                contentType: multipart/form-data
                explode: true
      responses:
        '201':
          description: successful operation
          content:
            application/json:
              schema:
                type: string
                format: uuid
                description: Node ID of the created shadow lensmanagement
Generation Details
Steps to reproduce

We are making use of openapi-generator-cli. Here is the script

openapi-generator-cli generate -i API_LINK -g typescript-fetch -o ~/projects/lens-graph/src/clients/lensManagement --additional-properties=typescriptThreePlus=true
Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/1905

Suggest a fix
adampax commented 1 year ago

It seems like the isFile value only gets set to true if and only if we have set the format of the lensData to binary. Has anybody else encountered this before?

I'm running into a similar issue. In my case, I do have a property with format: binary, but it is in a referenced schema, and not directly within the requestBody.

The workaround for me was to add a dummy property directly under the requestBody that has format: binary. This appears to force isFile to true and correctly generates the api to use FormData

jannesiera commented 1 year ago

We're also running into this issue. Not sure why the generated code isn't just let useForm = canConsumeForm; instead of let useForm = true;. Any fix for this in sight?

rkorzhoff commented 5 months ago

Any updates?