openapi-typescript
openapi-typescript copied to clipboard
`null` and `''` (empty string) are serializable to JSON and should therefore be considered a valid body payload
Description
Calling the client with body: null or body: '' will result in no body sent to the server (same behavior as with undefined).
I believe that they should be serialized to null and "" respectively as these are valid JSON values and should be sent to the server.
I believe the problem lies in this condition: https://github.com/drwpow/openapi-typescript/blob/19ab7342594df2ec7bb60d891818fe22b3e96e38/packages/openapi-fetch/src/index.js#L79-L81
where it should rather use strict comparison: requestInit.body !== undefined.
While the issue manifests only with null and '', the problem is bigger here as any other falsy value (false, 0) will bypass the body serializer too. They are sent "correctly" to the server, just because they fall back to default serialization provided by the browser, where null and '' is apparently considered as "no-body".
Reproduction
client.POST('/some-endpoint', {
body: null // or ''
})
results in no body being sent.
const client = createClient<paths>({
bodySerializer: body => {
console.log('serializing')
return JSON.stringify(body)
},
})
client.POST('/some-endpoint', {
body: 0 // or false
})
results in body being sent, but does not yield serializing in the console.
Expected result
All mentioned values go through the serializer and are sent to the server.
Checklist
- [x] I’m willing to open a PR (see CONTRIBUTING.md)
This logic makes sense! This seems like an improvement.