Skip to content

fix(openapi-fetch): handle empty error responses when Content-Length header is stripped#2605

Merged
drwpow merged 2 commits intoopenapi-ts:mainfrom
kaigritun:fix/empty-error-response-no-content-length
Feb 12, 2026
Merged

fix(openapi-fetch): handle empty error responses when Content-Length header is stripped#2605
drwpow merged 2 commits intoopenapi-ts:mainfrom
kaigritun:fix/empty-error-response-no-content-length

Conversation

@kaigritun
Copy link
Contributor

Summary

Fixes #2574

When a proxy (like Cloudflare) strips the Content-Length header from an error response with no body, the client would return an empty string as the error value. Since empty string is falsy, this causes issues when checking if (error) - the code incorrectly treats it as "no error".

Changes

This fix applies the same safe handling to error responses that already exists for success responses. When Content-Length is absent and the body is empty:

  • Use response.text() first to get the raw content
  • If the raw content is empty, return undefined for the error value
  • This is consistent with the existing handling for status 204 and Content-Length: 0

Testing

Added two new test cases in delete.test.ts:

  1. Error response (500) with empty body and no Content-Length header
  2. Success response (200) with empty body and no Content-Length header (verifies existing behavior still works)

All 442 tests pass including the new ones.

Reproduction

// Simulate proxy stripping Content-Length header
global.fetch = async () => new Response(null, {
  status: 500,
  headers: {} // No Content-Length header
});

// Before fix: { error: "", response } - empty string is falsy
// After fix: { error: undefined, response } - consistent with 204 handling

…header is stripped

When a proxy (like Cloudflare) strips the Content-Length header from an
error response with no body, the client would return an empty string
as the error value, which is falsy and causes issues when checking
`if (error)`.

This fix applies the same safe handling to error responses that already
exists for success responses - when Content-Length is absent and the
body is empty, return undefined for the error value to be consistent
with 204 and Content-Length: 0 handling.

Fixes openapi-ts#2574
@kaigritun kaigritun requested a review from a team as a code owner February 10, 2026 04:19
@kaigritun kaigritun requested a review from drwpow February 10, 2026 04:19
@netlify
Copy link

netlify bot commented Feb 10, 2026

👷 Deploy request for openapi-ts pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 37cf46f

@changeset-bot
Copy link

changeset-bot bot commented Feb 10, 2026

🦋 Changeset detected

Latest commit: 37cf46f

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

This PR includes changesets to release 2 packages
Name Type
openapi-fetch Patch
openapi-react-query 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

expect(response.ok).toBe(false);
});

test("handles success response with empty body when Content-Length header is stripped by proxy", async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Great tests! 🎉

@drwpow drwpow enabled auto-merge (squash) February 12, 2026 15:50
@drwpow drwpow merged commit b775891 into openapi-ts:main Feb 12, 2026
7 checks passed
@openapi-ts-bot openapi-ts-bot mentioned this pull request Feb 12, 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.

Fails to parse responses when Content-Length header is stripped by proxy

2 participants