Skip to content

fix(openapi-fetch): use text() when no content-length is provided to avoid errors parsing empty response (200 with no content)#2508

Merged
drwpow merged 1 commit intoopenapi-ts:mainfrom
srbarba:main
Feb 8, 2026
Merged

fix(openapi-fetch): use text() when no content-length is provided to avoid errors parsing empty response (200 with no content)#2508
drwpow merged 1 commit intoopenapi-ts:mainfrom
srbarba:main

Conversation

@srbarba
Copy link
Copy Markdown
Contributor

@srbarba srbarba commented Oct 19, 2025

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, calling response.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 when parseAs: "json". The fix:

  1. First reads the response as text using response.text()
  2. Then conditionally parses it as JSON only if content exists
  3. Returns undefined if the response is empty

How to Review

  1. Review the logic: Check the added condition in index.js around line 245 that handles the case when parseAs === "json" && !contentLength
  2. Run the test: The new test "json with empty response" in response.test.ts verifies the fix works correctly
  3. Verify no regressions: All existing tests should pass, confirming backward compatibility
  4. Consider edge cases: The fix only applies when:
    • Response is successful (2** except 204)
    • parseAs is set to "json" (default)
    • Content-Length header is absent (not just "0")

Key review points:

  • Empty responses now return { data: undefined } instead of throwing an error
  • Responses with Content-Length: "<any>" are still handled by the existing logic
  • The fix doesn't affect error responses or other parseAs options

Checklist

  • Unit tests updated
  • docs/ updated (if necessary) - No docs changes needed, internal bug fix
  • pnpm run update:examples run (only applicable for openapi-typescript) - N/A, this is openapi-fetch

@srbarba srbarba requested a review from a team as a code owner October 19, 2025 23:36
@srbarba srbarba requested a review from gzm0 October 19, 2025 23:36
@netlify
Copy link
Copy Markdown

netlify Bot commented Oct 19, 2025

👷 Deploy request for openapi-ts pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit a38257a

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Oct 19, 2025

🦋 Changeset detected

Latest commit: a38257a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
openapi-fetch Patch
openapi-react-query Patch
swr-openapi Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@srbarba
Copy link
Copy Markdown
Contributor Author

srbarba commented Oct 19, 2025

Related issue: #1977

Copy link
Copy Markdown
Contributor

@drwpow drwpow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Good tests, thank you!

@drwpow drwpow merged commit 89843b0 into openapi-ts:main Feb 8, 2026
7 checks passed
@openapi-ts-bot openapi-ts-bot mentioned this pull request Feb 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants