Skip to content
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

"Unexpected end of JSON input" with bun.js #1486

Closed
mihon73 opened this issue Dec 13, 2023 · 8 comments
Closed

"Unexpected end of JSON input" with bun.js #1486

mihon73 opened this issue Dec 13, 2023 · 8 comments
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library stale

Comments

@mihon73
Copy link

mihon73 commented Dec 13, 2023

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
@mihon73 mihon73 added bug Something isn't working openapi-fetch Relevant to the openapi-fetch library labels Dec 13, 2023
@mihon73
Copy link
Author

mihon73 commented Dec 13, 2023

openapi-fetch 0.8.2
bun 1.0.16

@titouv
Copy link

titouv commented Dec 16, 2023

Getting the same error with a bearer authorized request if that can help

@titouv
Copy link

titouv commented Dec 16, 2023

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.

@titouv
Copy link

titouv commented Dec 16, 2023

Seems like this issue is known in the Bun repo oven-sh/bun#6969 (comment) (fix in oven-sh/bun#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.

@drwpow drwpow mentioned this issue Jan 29, 2024
3 tasks
@sdekna
Copy link

sdekna commented Feb 4, 2024

I experienced this when using Bun. There is a pull request that should fix this and should be merged soon hopefully:
oven-sh/bun#8668

A quick workaround meanwhile:

  • open ./node_modules/openapi-fetch/dist/index.js
  • replace const cloned = response.clone() with const cloned = response

@drwpow
Copy link
Contributor

drwpow commented Feb 4, 2024

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).

Copy link
Contributor

github-actions bot commented Aug 6, 2024

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.

@github-actions github-actions bot added the stale label Aug 6, 2024
Copy link
Contributor

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.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 14, 2024
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 stale
Projects
None yet
Development

No branches or pull requests

4 participants