Skip to content

Coalescing response data to empty object breaks type safety #3668

@twavv

Description

@twavv

Description

The client generated code includes a bit like

      // ...fetch data via Axios...
      return {
        ...response,
        data: data ?? {},
      };

If the API endpoint returns null then the response gets silently coalesced to the empty object {}.

This breaks code that actually expects to handle that null:

const res = await getSomething({ ..., throwOnError: true });

const data: Something | null = res.data;

// This check never actually fails because `null` gets coalesced to `{}`
// which is always truthy.
if (!data) {
  return "Something not found!";
}

return `Got ${data.id}.`;

Reproducible example or configuration

Not applicable with the Pet Store API.

To reproduce, you need an endpoint that returns null. This isn't the most common thing in the world (usually you end up returning 404 instead of just 200 with a null response body).

OpenAPI specification (optional)

No response

System information (optional)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🔥Broken or incorrect behavior.needs info ⏳More information needed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions