Skip to content

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

Closed
@goce-cz

Description

@goce-cz

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    PRs welcomePRs are welcome to solve this issue!bugSomething isn't workinggood first issueStraightforward problem, solvable for first-time contributors without deep knowledge of the projectopenapi-fetchRelevant to the openapi-fetch library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions