Skip to content

Data is null, not undefined when 204 or content-length 0 #2464

@mokkabonna

Description

@mokkabonna

openapi-react-query version

0.5.0

Description

This is my hook: data is typed to be etiher the expected data or undefined.

Image

But we experienced at 403 response that had no body (so content length 0) and then suddenly data was null. Causing an error further down in the code due to passing it to a function with default param. And they only work for undefined, not null.

Tracked it down to this line: https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-react-query/src/index.ts#L201

This should be data ?? undefined for the types to be correct, or the types need to reflect null is possible. The first option is the intended behavior, no?

Even the test for this does not make sense if you see the type for data and the assertion to be null, not undefined.

Image

I first assumed an error in openapi-fetch. But it correctly returns no data (so undefined) https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-fetch/src/index.js#L231

Reproduction

Can be reproduced by responding with no body and a non 2xx response. Log data.

Expected result

Data should always log undefined for any non 2xx response. It logs null.

Extra

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingopenapi-react-queryRelevant to openapi-react-query

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions