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.04k stars 6.38k forks source link

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

Open ddurham2 opened 4 years ago

ddurham2 commented 4 years ago

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.

EBrozzi commented 7 months ago

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?