Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

null and '' (empty string) are serializable to JSON and should therefore be considered a valid body payload #1695

Open
1 task done
goce-cz opened this issue Jun 14, 2024 · 1 comment
Labels
bug Something isn't working good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project openapi-fetch Relevant to the openapi-fetch library PRs welcome PRs are welcome to solve this issue!

Comments

@goce-cz
Copy link

goce-cz commented Jun 14, 2024

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

@goce-cz goce-cz added bug Something isn't working openapi-fetch Relevant to the openapi-fetch library labels Jun 14, 2024
@drwpow drwpow added PRs welcome PRs are welcome to solve this issue! good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project labels Jun 19, 2024
@drwpow
Copy link
Contributor

drwpow commented Jun 19, 2024

This logic makes sense! This seems like an improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project openapi-fetch Relevant to the openapi-fetch library PRs welcome PRs are welcome to solve this issue!
Projects
None yet
Development

No branches or pull requests

2 participants