OpenAPITools / OpenAPITools/openapi-generator

[BUG] [angular-typescript] JSON object has a filename of "blob" in HTTP request multipart form data

Open
#5,673 1 comment 3 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

Hello,
This is related to the fix for bug [#2733]

For the typescript-angular generator, there is an additional issue with the code generated, when using a multipart/form-data object.

I'm using the latest version as of this writing

Declare a POST method with the following requestBody:

'/test/upload':
post:
requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                foo:
                  type: object
                  properties:
                    f1:
                      type: string
                    f2:
                      type: string
                fileUpload:
                  type: string
                  format: binary

The generated service works, but the request body's foo object has a filename.

  ------WebKitFormBoundaryVXPcUvxGz0M5kWFb
  Content-Disposition: form-data; name="foo"; filename="blob"
  Content-Type: application/json
...

The problem is that filename="blob" value. It is making some backends think I'm uploading a file here.

The generated code looks something like:

  formParams = formParams.append('foo', useForm ? new Blob([JSON.stringify(foo)], { type: 'application/json' }) : foo) || formParams;

Blob() is naming itself "blob", but the problem for these backends goes away when I change the generated code to:

  formParams = formParams.append('foo', useForm ? new Blob([JSON.stringify(foo)], { type: 'application/json' }) : foo, "") || formParams;

... where I have explicitly given an empty filename to the append() method.

The request body now looks like

  ------WebKitFormBoundaryVXPcUvxGz0M5kWFb
  Content-Disposition: form-data; name="foo"; filename=""
  Content-Type: application/json
...

Otherwise, how is a backend to know whether it's to pass a real file on disk (named "blob") to the handler or an in memory object?

I'll be glad to take a stab at creating a PR unless there are obvious objections to making such a change.

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 in the typescript-angular generator and inspect the generated service code that appends multipart/form-data fields through formParams. Reproduce the request with the provided schema, then verify that object parts no longer receive the default "blob" filename while file uploads still work.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.