hey-api / openapi-ts

🚀 The OpenAPI to TypeScript codegen. Generate clients, SDKs, validators, and more. Support: @mrlubos
https://heyapi.dev
Other
1.24k stars 96 forks source link

FormData Breaks Headers #1179

Open mannanj opened 1 day ago

mannanj commented 1 day ago

Description

Using a request body of type content: multipart/form-data breaks the headers generated by hey-api for solid query.

Compared to other methods that do not use form data, the library decides to generate a client post method that overwrites all headers (incorrectly) in order to set the content-type to null to respect browser boundaries.

Example: (broken headers behavior)

export const methodName = <ThrowOnError extends boolean = false>(options: Options<MethodDataType, ThrowOnError>, ) => {
return (options?.client ?? client).post<
MethodNameResponseType,
MethodNameErrorType,
ThrowOnError>({

...options,
...formDataBodySerializer,
headers: { 
  'Content-Type': null,
}
url: <url here>,
});
};

This behavior makes it so the method ignores all headers passed in, such as required Accept headers.

This is what should happen: (What I do to fix the behavior is pass the options.headers through spread syntax).

export const methodName = <ThrowOnError extends boolean = false>(options: Options<MethodDataType, ThrowOnError>, ) => {
return (options?.client ?? client).post<
MethodNameResponseType,
MethodNameErrorType,
ThrowOnError>({

...options,
...formDataBodySerializer,
headers: {  
  ...options.headers, // FIX HERE
  'Content-Type': null,
}
url: <url here>,
});
};

Reproducible example or configuration

import { defineConfig } from '@hey-api/openapi-ts';

defineConfig({
client: '@hey-api/client-fetch',
input: '<path to yaml>',
output: { path: '<path to output>', format: 'prettier'}
plugins: ['@tanstack/solid-query']
})

OpenAPI specification (optional)

< endpoint url >:
post:
summary: <summary>
operationId: <name here>
parameters: 
   ... can be provided upon request. they are just 3 string parameters in the path that are required.
 -$ref: <ref to accept header here>
 -$ref: <ref to source header here>

requestBody: 
required: true
content: 
multipart/form-data:
schema: 
type: object

System information (optional)

node v18.20.1 @hey-api/openapi-ts ^0.53 @hey-api/client-fetch ^0.24

@tanstack/solid-query plug.

Note: It does not matter what headers I specify in the .yaml, the method generated for the form data does not respect them.

mrlubos commented 1 day ago

Hey @mannanj, are you sure you're on the latest version? This has been fixed in https://github.com/hey-api/openapi-ts/releases/tag/%40hey-api/openapi-ts%400.53.2