kubb-labs / kubb

OpenAPI to TypeScript, React-Query, Zod, Zodios, Faker.js, MSW and Axios.
https://kubb.dev
MIT License
568 stars 40 forks source link

SwaggerClient does not respect "multipart/form-data" and send default "application/json" with axios #1049

Closed vkrepkiy closed 2 weeks ago

vkrepkiy commented 1 month ago

What version of kubb is running?

@kubb/core@2.20.0

What platform is your computer?

MacOs

What version of external packages are you using(@tanstack-query, MSW, React, Vue, ...)

"@kubb/cli": "^2.20.0", "@kubb/core": "^2.20.0", "@kubb/swagger": "^2.20.0", "@kubb/swagger-client": "^2.20.0", "@kubb/swagger-faker": "^2.20.0", "@kubb/swagger-ts": "^2.20.0",

What steps can reproduce the bug?

create swagger.yaml (important part is to set multipart/form-data as a requestBody contentType

openapi: 3.0.0
info:
  title: Minimal API
  description: A minimal API with a multipart/form-data file upload example
  version: 1.0.0

paths:
  /upload:
    post:
      summary: Upload a file
      description: Upload a file using multipart/form-data
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                file:
                  type: string
                  format: binary
                  description: The file to upload
                description:
                  type: string
                  description: A description of the file
            encoding:
              file:
                contentType: application/octet-stream
      responses:
        '200':
          description: Successfully uploaded
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  message:
                    type: string
                    example: File uploaded successfully
        '400':
          description: Bad request
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: false
                  message:
                    type: string
                    example: Bad request

components:
  schemas:
    UploadResponse:
      type: object
      properties:
        success:
          type: boolean
        message:
          type: string

create kubb.config.ts

import { defineConfig } from '@kubb/core';
import { pluginOas } from '@kubb/plugin-oas';
import { pluginClient } from '@kubb/swagger-client';
import { pluginTs } from '@kubb/swagger-ts';
import { join } from 'path';

export default defineConfig({
  root: __dirname,
  input: {
    path: join(__dirname, './swagger.yaml'),
  },
  output: {
    path: join(__dirname, './src/lib-generated-tmp'),
    clean: true,
  },
  plugins: [
    pluginOas({
      output: {
        path: './schema',
      },
      validate: true,
      serverIndex: 0,
    }),
    pluginTs(),
    pluginClient({
      output: {
        path: './axios',
        exportType: 'barrelNamed',
      },
      dataReturnType: 'data',
      client: {
        importPath: '../../client',
      },
    })
  ],
});

run npx kubb --config kubb.config.ts

As a result you have

export async function postUpload(data?: PostUploadMutationRequest, options: Partial<Parameters<typeof client>[0]> = {}): Promise<ResponseConfig<PostUploadMutationResponse>["data"]> {
    const res = await client<PostUploadMutationResponse, PostUploadMutationRequest>({ method: "post", url: `/upload`, data, ...options });
    return res.data;
}

which would try to send just an application/json instead of packing data to FormData before making a request

How often does this bug happen?

Every time

What is the expected behavior?

Should it pack data for multipart/form-data requests to FormData object before passing it to axios?

Swagger/OpenAPI file?

No response

Additional information

No response

stijnvanhulle commented 2 weeks ago

V2.21.2 will also add the same logic for FormData for the client plugin.