Skip to content

Conversation

srbarba
Copy link

@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
Copy link

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

Copy link

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
Author

srbarba commented Oct 19, 2025

Related issue: #1977

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.

1 participant