hey-api / openapi-ts

šŸš€ The OpenAPI to TypeScript codegen. Generate clients, SDKs, validators, and more. Support: @mrlubos
https://heyapi.dev
Other
1.34k stars 103 forks source link

multipart upload in node+axios fails #210

Closed muka closed 7 months ago

muka commented 7 months ago

Describe the bug

Hi, I am trying this with node v20.x uploading to a remote server.

The generated client uses axios and has multipart formData generated field.

I tried to populate the object with fields and a blob, but server side I got an empty file upload.

This ended up working

     const file = new Blob([await readFile(filename)], {
       type: lookup(filename),
     })
    // NOTE *not* using 'form-data' or it fails with 'TypeError: source.on is not a function'
    const formData = new FormData();
    formData.append("file", file); // Blob
    for (const key in model) {
      formData.append(key, model[key]);
    }

   await client.ui.saveBackground({
    formData: formData as any,
  }),

my client package.json

"axios": "^1.6.8",
"@hey-api/openapi-ts": "^0.33.2",

the error from the client (but seems from axios actually)

 AxiosError: Blob is not supported. Use a Buffer instead.
    at convertValue (.../node_modules/axios/lib/helpers/toFormData.js:124:13)
    at FormData.defaultVisitor (.../node_modules/axios/lib/helpers/toFormData.js:175:49)
    ...

Using a Buffer result in an empty file server side.

Using a FormData as input still works. Why not just allow FormData type for node+axios ?

OpenAPI specification

        "/api/ui/asset/backgrounds": {
            "post": {
                "operationId": "saveBackground",
                "deprecated": false,
                "parameters": [],
                "requestBody": {
                    "required": true,
                    "content": {
                        "multipart/form-data": {
                            "schema": {
                                "type": "object",
                                "properties": {
                                    "filename": {
                                        "type": "string"
                                    },
                                    "appId": {
                                        "type": "string"
                                    },
                                    "type": {
                                        "type": "string",
                                        "enum": [
                                            "models",
                                            "backgrounds",
                                            "animations",
                                            "documents"
                                        ]
                                    },
                                    "userId": {
                                        "type": "string"
                                    },
                                    "ts": {
                                        "format": "date-time",
                                        "type": "string"
                                    },
                                    "metadata": {
                                        "$ref": "#/components/schemas/UIAssetMetadataDto"
                                    },
                                    "file": {
                                        "type": "file"
                                    }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": ""
                    }
                },
                "tags": [
                    "UI"
                ],
                "security": [
                    {
                        "bearer": []
                    }
                ]
            }
        },

Client

Axios

Generated client

https://github.com/sermas-eu/sermas-api-client

System information

ubuntu 22.4
node 20.x

Severity

annoyance

Additional information

Discussed in comments in https://github.com/hey-api/openapi-ts/issues/29

jordanshatford commented 7 months ago

@muka thanks for creating the issue. Do you know if this issue is present in the node (with node-fetch) or fetch clients? I believe this is axios+node only but want to verify

mrlubos commented 7 months ago

@muka is this a regression?

muka commented 7 months ago

@jordanshatford in my case it is, do you want me to try with the node+fetch ?

@mrlubos not sure, switched to this lib a day ago

jordanshatford commented 7 months ago

@muka if you could that would be great. I have put up a PR that does fix your issue using axios+node but need to discuss further before it is merged and released

I did check node myself and did not see the error, I was able to reproduce with axios and after my PR changes it was not longer throwing error

muka commented 7 months ago

Tried and with fetch client work without the issue. Thank you!

jordanshatford commented 7 months ago

@muka glad to hear that. the latest version released today should have that fixed for axios aswell if you prefer it!