redux-toolkit icon indicating copy to clipboard operation
redux-toolkit copied to clipboard

rtk-query codegen doesnt process multipart form data types correctly

Open stckcrsh opened this issue 3 years ago • 4 comments

We are using the open-api codegen to create the api code for our app and we have an endpoint that takes in a file upload using mulitpart/form-data when we generate types from this it causes typescript failures. It seems that the generated code is not expecting the FormData object but instead an object that contains blobs.

Swagger snippet

  ...
    "/favicon": {
      "post": {
        "description": "Upload Favicon",
        "produces": ["application/json"],
        "consumes": ["multipart/form-data"],
        "parameters": [
          {
            "in": "formData",
            "name": "favicon",
            "type": "file",
            "required": true
          }
        ], 
  ...

Generated baseApi

export type PostFaviconApiArg = {
  body: {
    favicon: Blob;
  };
};

This doesnt work and we have to surround the code with //@ts-ignore to get it to work.

The generated type should be

export type PostFaviconApiArg = {
  body: FormData;
};

When i manually change it to this then everything works and file uploads happen as expected.

stckcrsh avatar Dec 13 '21 18:12 stckcrsh

I also found that if i cast my data with unknown and then the type expected that works, but i feel like that is just as bad as //@ts-ignore

const faviconData = new FormData();
faviconData.append("favicon", favicon);
submitFavicon({ body: faviconData as unknown as { favicon: Blob } });

stckcrsh avatar Dec 13 '21 18:12 stckcrsh

I think codegen ignores the requestBody.content attribute of the schema and simply assumes it's application/json while in some cases (e.g., JWT) it must be application/x-www-form-urlencoded.

"paths": {
    "/auth/jwt/login": {
      "post": {
        "tags": [
          "auth"
        ],
        "summary": "Auth:Jwt.Login",
        "operationId": "auth_jwt_login_auth_jwt_login_post",
        "requestBody": {
          "content": {
            "application/x-www-form-urlencoded": {
              "schema": {
                "$ref": "#/components/schemas/Body_auth_jwt_login_auth_jwt_login_post"
              }
            }
          },
          "required": true
        },

phretor avatar Feb 09 '22 11:02 phretor

Would be great if rtk could conform to the OpenAPI spec in this case. This is a missing feature we are currently working around at my company.

mankittens avatar May 13 '23 00:05 mankittens

It seems related to #3063

dzegarra avatar May 22 '23 15:05 dzegarra