redux-toolkit
redux-toolkit copied to clipboard
rtk-query codegen doesnt process multipart form data types correctly
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.
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 } });
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
},
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.
It seems related to #3063