openapi-typescript
openapi-typescript copied to clipboard
SyntaxError: Unexpected end of JSON input
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
- [ ] I’m willing to open a PR (see CONTRIBUTING.md)
#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.
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 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 :(
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.
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
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)
Another way to fix it add
@ApiBody({ schema: {} })
and send body: {}. But I m not sure that its correct way.
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.
Hi, I am also experiencing this issue with a response status 202.
It should also be noted that we respect the
Content-Lengthheader, 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
RestClientandRestTemplate, mostClientHttpRequestFactoryimplementations 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 aContent-Lengthheader is no longer set.
So this means that adding the header is effectively removed by SpringBoot.
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.