openapi-typescript
openapi-typescript copied to clipboard
"Unexpected end of JSON input" with bun.js
Hey there, thanks for your work. Looks like needed one more fix for bun (with node works good).
Description If fetch with the ban, it looks like an object that has not yet been fully received is cloned.
86 | }
87 | const cloned = response.clone();
88 | return {
89 | data:
90 | typeof cloned[parseAs] === "function"
91 | ? await cloned[parseAs]()
^
SyntaxError: Unexpected end of JSON input
at /someLongPath/project/node_modules/openapi-fetch/dist/index.js:91:21
Reproduction error occur with using bun only. client configuration:
const {GET} = createClient<paths>({
baseUrl: 'https://some.awesome.api.endpoint/api/3.1',
headers: {
Authorization: "Bearer " + super_secret_token,
"Content-type": "plain/text",
},
})
The cloned response and the original are different, there is with some debug info before the error line:
in code:
// await Bun.sleep(3000)
const cloned = response.clone()
const clonedText = await cloned.text()
const responseText = await response.text()
console.log('cloned:', clonedText.length)
console.log('response:', responseText.length)
process.exit(0)
return {
data:
typeof cloned[parseAs] === "function"
in output:
cloned: 0
response: 56084
And if we wait a little before cloning: in code:
await Bun.sleep(3000)
const cloned = response.clone()
const clonedText = await cloned.text()
const responseText = await response.text()
console.log('cloned:', clonedText.length)
console.log('response:', responseText.length)
process.exit(0)
return {
data:
typeof cloned[parseAs] === "function"
in output:
cloned: 56084
response: 56084
openapi-fetch 0.8.2 bun 1.0.16
Getting the same error with a bearer authorized request if that can help
After investigating it seems the problem is from the clone before getting the json from the body (at least in my case), removing the cloning (and probably breaking other functionalities) works for me.
Seems like this issue is known in the Bun repo https://github.com/oven-sh/bun/issues/6969#issuecomment-1847705004 (fix in https://github.com/oven-sh/bun/pull/6468 but not yet merged)
Maybe it could be considered to add a temporary fix here because fixing this type of bug doesn't seem to be a priority for bun maintainers. Also because it is a major bug making the library unusable with bun.
I experienced this when using Bun. There is a pull request that should fix this and should be merged soon hopefully: https://github.com/oven-sh/bun/pull/8668
A quick workaround meanwhile:
- open
./node_modules/openapi-fetch/dist/index.js
- replace
const cloned = response.clone()
withconst cloned = response
Ah glad to see they are fixing it! In the upcoming 0.9.0
(which will likely have some very minor breakages), I decided to remove all internal response.clone()
-ing because it can be a performance bottleneck. I’d love for consumers of openapi-fetch to just learn to call response.clone()
themselves when needed (most people won’t ever need to, and for advanced usecases it’s better to learn about the native fetch API than shielding you from it).
This issue is stale because it has been open for 90 days with no activity. If there is no activity in the next 7 days, the issue will be closed.
This issue was closed because it has been inactive for 7 days since being marked as stale. Please open a new issue if you believe you are encountering a related problem.