Closed DjordyKoert closed 1 week ago
Thanks for raising! This is definitely a bug. It’s caused by a | never
in the union that throws a wrench into the type inference, but this should be avoidable. Possibly even with a NonNullable<T>
wrapping the data
Came here to report similar issue. I noticed this started showing when upgrading openapi-fetch
to 0.10.0 or higher (latest 0.12.x also affected).
Using openapi-typescript
7.4.1
Hi, I have a similar problem
generated schema.d.ts:
export interface operations {
...
deleteLoadBalancer: {
parameters: { ... };
requestBody?: never;
responses: {
/** @description OK */
200: {
headers: {
[name: string]: unknown;
};
content?: never;
};
};
};
...
}
calling api:
const { data } = await client.DELETE('/load-balancers/{name}', {
params: {
header: { correlationId: generateCorrelationId() },
path: { name: loadBalancerName },
query: { projectId },
},
// parseAs: 'text',
});
throws error:
SyntaxError: Unexpected end of JSON input
at u (index.js:164:44)
which is in this part of library:
// parse response (falling back to .text() when necessary)
if (response.ok) {
// if "stream", skip parsing entirely
if (parseAs === "stream") {
return { data: response.body, response };
}
return { data: await response[parseAs](), response }; // line 164
}
Calling the API with parseAs: 'text',
solves the error but I don't know whether it is the correct solution
I feel like this should have been caught by this:
// handle empty content
if (response.status === 204 || response.headers.get("Content-Length") === "0") {
return response.ok ? { data: undefined, response } : { error: undefined, response };
}
but this API doesn't return Content-Length header nor have 204 status
Description
Having a
204
response in combination with a200
response in the generated type causes the returned data to becomeundefined
Generated
openapi.ts
file byopenapi-typescript
:Reproduction
Have an operation as defined above (with a
200
and a204
with no content). Then attempt to use the openapi-fetch client for the operation.Expected result
I expected the type a union of
components["schemas"]["OrderDTO"][] | undefined
and for me to not have to do a manual type assertion.Checklist