openapi-typescript icon indicating copy to clipboard operation
openapi-typescript copied to clipboard

SyntaxError: Unexpected end of JSON input

Open Rykuno opened this issue 2 years ago • 10 comments

Looks like https://github.com/drwpow/openapi-typescript/pull/1280 might fix this when merged.

Description With the update to 0.7.1 from 0.6.0, errors began to throw with SyntaxError: Unexpected end of JSON input when a record from the API returned null/undefined.

A brief description of the bug. Provide either a screenshot or the full error message

web:dev: SyntaxError: Unexpected end of JSON input
web:dev:     at JSON.parse (<anonymous>)

Reproduction How can this be reproduced / when did the error occur? Does the issue occur in a specific browser, or all browsers?

Expected result Expect null/undefined rather than failing to parse json

(in case it’s not obvious)

Checklist

Rykuno avatar Aug 09 '23 02:08 Rykuno

#1280 is unrelated and is on the typegen side; that won’t affect a runtime error like what you’re seeing.

So I’m assuming that your API does NOT return a 204 No content status, but instead, returns a 200 where the top-level response may be null. In that case, I’m not sure if we handle that. I guess this is a bug? But this is a curious API choice to return 200 with a null or undefined response at the root level. But technically not invalid.

drwpow avatar Aug 09 '23 02:08 drwpow

It should also be noted that we respect the Content-Length header, so if your API sent "Content-Length": "0" then we wouldn’t try and parse JSON in that instance, either.

drwpow avatar Aug 09 '23 02:08 drwpow

We have this issue in two areas w/ the new version. The issue is this is happening for responses from two different very popular public API's; so we have no control over the implementation :(

Rykuno avatar Aug 09 '23 14:08 Rykuno

I have a 202 response with Content-Type: text/plain and Content-Length: 27, which is a message. And I'm getting a JSON parse error as well.

tuler avatar Sep 06 '23 21:09 tuler

got the same error:

Error: 26 |         // parse response (falling back to .text() when necessary)
27 |         if (response.ok) {
28 |             let data = response.body;
29 |             if (parseAs !== "stream") {
30 |                 const cloned = response.clone();
31 |                 data = typeof cloned[parseAs] === "function" ? await cloned[parseAs]() : await cloned.text();
                                                                         ^
SyntaxError: Unexpected end of JSON input

KadoBOT avatar Sep 15 '23 04:09 KadoBOT

Get the same error on last version for PATCH method with empty body

So if I send body: {} its works fine, but i have red line under because generated API not expected. (Response return 204) If I remove body: {} its display me Unexpected end of JSON input (Response return 400)

StagnantIce avatar Oct 04 '23 07:10 StagnantIce

Another way to fix it add

@ApiBody({ schema: {} })

and send body: {}. But I m not sure that its correct way.

StagnantIce avatar Oct 04 '23 08:10 StagnantIce

I faced with this issue when API server returns a response with status 202 and there is neither a body nor content-length header. As a workaround, I override the parse strategy (parseAs: 'stream') for a specific method to avoid the issue.

yethee avatar May 29 '24 18:05 yethee

Hi, I am also experiencing this issue with a response status 202.

It should also be noted that we respect the Content-Length header, so if your API sent "Content-Length": "0" then we wouldn’t try and parse JSON in that instance, either.

We looked at this as a solution to the problem, but unfortunately it will not work as our backend API is incapable of doing this without significant memory increases.

https://github.com/spring-projects/spring-framework/wiki/Upgrading-to-Spring-Framework-6.x#web-applications

To reduce memory usage in RestClient and RestTemplate, most ClientHttpRequestFactory implementations no longer buffer request bodies before sending them to the server. As a result, for certain content types such as JSON, the contents size is no longer known, and a Content-Length header is no longer set.

So this means that adding the header is effectively removed by SpringBoot.

brandonp-ais avatar Jun 17 '24 01:06 brandonp-ais

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[bot] avatar Sep 15 '24 02:09 github-actions[bot]