OpenAPITools / OpenAPITools/openapi-generator

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

Open
#9,850 3 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
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
  • Honour the multipart-formdata content-type. Currently it is only looking at if the format is binary. what if a user wants to send string in the multipart-formdata?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the typescript-fetch template containing the shown canConsumeForm and isFile logic, then reproduce the issue using the supplied OpenAPI YAML and openapi-generator-cli command. Done means a generated client sends the multipart/form-data request when the form field uses format byte or another non-binary string format, with regression coverage for the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.