Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/openapi-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,12 @@ export default function createClient(clientOptions) {
}

// handle empty content
if (response.status === 204 || request.method === "HEAD" || response.headers.get("Content-Length") === "0") {
if (
response.status === 204 ||
request.method === "HEAD" ||
(response.headers.get("Content-Length") === "0" &&
!response.headers.get("Transfer-Encoding")?.includes("chunked"))
) {
return response.ok ? { data: undefined, response } : { error: undefined, response };
}

Expand Down
32 changes: 32 additions & 0 deletions packages/openapi-fetch/test/common/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,36 @@ describe("response", () => {
}
});
});

describe("chunked transfer with zero Content-Length", () => {
test("does not treat chunked body with Content-Length: 0 as empty", async () => {
const mock = [{ id: 1 }];
const client = createObservedClient<paths>({}, async () =>
Response.json(mock, {
status: 200,
headers: { "Content-Length": "0", "Transfer-Encoding": "chunked" },
}),
);

const { data, error, response } = await client.GET("/resources");
expect(response.status).toBe(200);
expect(error).toBeUndefined();
expect(data).toEqual(mock);
});
});
describe("Content-Length: 0 without chunked", () => {
test("treats as empty when not chunked", async () => {
const client = createObservedClient<paths>({}, async () =>
Response.json([{ id: 1 }], {
status: 200,
headers: { "Content-Length": "0" },
}),
);

const { data, error, response } = await client.GET("/resources");
expect(response.status).toBe(200);
expect(error).toBeUndefined();
expect(data).toBeUndefined();
});
});
});