fix(openapi-fetch): use text() when no content-length is provided to avoid errors parsing empty response (200 with no content) #2508
+88
−6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes
Fixes an issue where responses with HTTP 200 status and no
Content-Length
header would fail when parsing JSON, even when the response body is empty.Problem: When a server returns a successful response (200 OK) without a
Content-Length
header and an empty body, callingresponse.json()
directly throws an error because there's no content to parse. This is a valid HTTP scenario but was causing runtime errors.Solution: Added a check for missing
Content-Length
headers whenparseAs: "json"
. The fix:response.text()
undefined
if the response is emptyHow to Review
index.js
around line 245 that handles the case whenparseAs === "json" && !contentLength
"json with empty response"
inresponse.test.ts
verifies the fix works correctlyparseAs
is set to"json"
(default)Content-Length
header is absent (not just"0"
)Key review points:
{ data: undefined }
instead of throwing an errorContent-Length: "<any>"
are still handled by the existing logicparseAs
optionsChecklist
docs/
updated (if necessary) - No docs changes needed, internal bug fixpnpm run update:examples
run (only applicable for openapi-typescript) - N/A, this is openapi-fetch