Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ddurham2 opened this issue Mar 23, 2020 · 1 comment

Comments

@ddurham2
Copy link
Contributor

ddurham2 commented Mar 23, 2020

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.

@ddurham2 ddurham2 changed the title [BUG] Description [BUG] [angular-typescript] JSON object has a filename of "blob" in HTTP request multipart form data Mar 23, 2020
@EBrozzi
Copy link

EBrozzi commented Jan 11, 2024

What is the state of this bug? Is there a workaround? Is there a property to set?

https://github.com/OpenAPITools/openapi-generator/blob/69e72203aeca023cb2f3bd392d3c080af6e2a0fe/modules/openapi-generator/src/main/resources/typescript-angular/api.service.mustache#L353C15-L353C15

why do you parse the object as Blob if the type is a Model? why not just JSON.stringify it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants