Skip to content

If error responses do not declare a content type, then success responses can be typed as undefined. #2350

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

Open
1 task done
dwjohnston opened this issue Jun 4, 2025 · 0 comments
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library

Comments

@dwjohnston
Copy link

openapi-fetch version

0.14.0

Description

In a scenario where error response do not have content shapes defined, then the type narrowing behaviour for success responses stops working correctly and can show as undefined.

Reproduction

I have a reproduction for this here:

https://github.com/dwjohnston/openapi-typescript-example/tree/missing-error-data

I have an spec defines the response for GET /pets/{petId} that looks like this:

                "responses": {
                    "200": {
                        "description": "successful operation",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Pet"
                                }
                            }
                        }
                    },
                    "500": {
                        "description": "Invalid ID supplied"
                    }
                },

Note no content is defined for the error response.

This will generate typings like this:

        responses: {
            /** @description successful operation */
            200: {
                headers: {
                    [name: string]: unknown;
                };
                content: {
                    "application/json": components["schemas"]["Pet"];
                };
            };
            /** @description Invalid ID supplied */
            500: {
                headers: {
                    [name: string]: unknown;
                };
                content?: never;
            };
        };

When I use the typings with OpenPI fetch then :

async function getPetById(id: number) : Promise< components["schemas"]["Pet"]>{
    const result= await client.GET("/pet/{petId}", {
        "params": {
            "path" :{
                "petId": id
            }
        }
    });

    
    
    if (result.error) {
        console.error("Error fetching pets:", result.error);
        throw new Error(result.error);
    }

    
    // Data can be undefined
    return result.data;
}

Expected result

Given the type guard where we have removed the error scenarios, the result.data type should definitely exist.

Extra

@dwjohnston dwjohnston added bug Something isn't working openapi-fetch Relevant to the openapi-fetch library labels Jun 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library
Projects
None yet
Development

No branches or pull requests

1 participant