fix(openapi-fetch): use text() when no content-length is provided to avoid errors parsing empty response (200 with no content) #2508
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-Lengthheader 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-Lengthheader 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-Lengthheaders whenparseAs: "json". The fix:response.text()undefinedif the response is emptyHow to Review
index.jsaround line 245 that handles the case whenparseAs === "json" && !contentLength"json with empty response"inresponse.test.tsverifies the fix works correctlyparseAsis set to"json"(default)Content-Lengthheader is absent (not just"0")Key review points:
{ data: undefined }instead of throwing an errorContent-Length: "<any>"are still handled by the existing logicparseAsoptionsChecklist
docs/updated (if necessary) - No docs changes needed, internal bug fixpnpm run update:examplesrun (only applicable for openapi-typescript) - N/A, this is openapi-fetch