Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/tame-dolives-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

**plugin(@hey-api/client-next)**: fix: JSON parsing error on empty response bodies without Content-Length header
5 changes: 5 additions & 0 deletions .changeset/tame-olives-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

**plugin(@hey-api/client-fetch)**: fix: JSON parsing error on empty response bodies without Content-Length header
5 changes: 5 additions & 0 deletions .changeset/tame-solives-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

**plugin(@hey-api/client-ky)**: fix: JSON parsing error on empty response bodies without Content-Length header
8 changes: 7 additions & 1 deletion examples/openapi-ts-fastify/src/client/client/client.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,16 @@ export const createClient = (config: Config = {}): Client => {
case 'arrayBuffer':
case 'blob':
case 'formData':
case 'json':
case 'text':
data = await response[parseAs]();
break;
case 'json': {
// Some servers return 200 with no Content-Length and empty body.
// response.json() would throw; read as text and parse if non-empty.
const text = await response.text();
data = text ? JSON.parse(text) : {};
break;
}
case 'stream':
return opts.responseStyle === 'data'
? response.body
Expand Down
8 changes: 7 additions & 1 deletion examples/openapi-ts-fetch/src/client/client/client.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,16 @@ export const createClient = (config: Config = {}): Client => {
case 'arrayBuffer':
case 'blob':
case 'formData':
case 'json':
case 'text':
data = await response[parseAs]();
break;
case 'json': {
// Some servers return 200 with no Content-Length and empty body.
// response.json() would throw; read as text and parse if non-empty.
const text = await response.text();
data = text ? JSON.parse(text) : {};
break;
}
case 'stream':
return opts.responseStyle === 'data'
? response.body
Expand Down
8 changes: 7 additions & 1 deletion examples/openapi-ts-ky/src/client/client/client.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,16 @@ export const createClient = (config: Config = {}): Client => {
case 'arrayBuffer':
case 'blob':
case 'formData':
case 'json':
case 'text':
data = await response[parseAs]();
break;
case 'json': {
// Some servers return 200 with no Content-Length and empty body.
// response.json() would throw; read as text and parse if non-empty.
const text = await response.text();
data = text ? JSON.parse(text) : {};
break;
}
case 'stream':
return opts.responseStyle === 'data'
? response.body
Expand Down
8 changes: 7 additions & 1 deletion examples/openapi-ts-next/src/client/client/client.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,16 @@ export const createClient = (config: Config = {}): Client => {
case 'arrayBuffer':
case 'blob':
case 'formData':
case 'json':
case 'text':
data = await response[parseAs]();
break;
case 'json': {
// Some servers return 200 with no Content-Length and empty body.
// response.json() would throw; read as text and parse if non-empty.
const text = await response.text();
data = text ? JSON.parse(text) : {};
break;
}
case 'stream':
return {
data: response.body,
Expand Down
8 changes: 7 additions & 1 deletion examples/openapi-ts-openai/src/client/client/client.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,16 @@ export const createClient = (config: Config = {}): Client => {
case 'arrayBuffer':
case 'blob':
case 'formData':
case 'json':
case 'text':
data = await response[parseAs]();
break;
case 'json': {
// Some servers return 200 with no Content-Length and empty body.
// response.json() would throw; read as text and parse if non-empty.
const text = await response.text();
data = text ? JSON.parse(text) : {};
break;
}
case 'stream':
return opts.responseStyle === 'data'
? response.body
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,16 @@ export const createClient = (config: Config = {}): Client => {
case 'arrayBuffer':
case 'blob':
case 'formData':
case 'json':
case 'text':
data = await response[parseAs]()
break
case 'json': {
// Some servers return 200 with no Content-Length and empty body.
// response.json() would throw; read as text and parse if non-empty.
const text = await response.text()
data = text ? JSON.parse(text) : {}
break
}
case 'stream':
return opts.responseStyle === 'data'
? response.body
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,16 @@ export const createClient = (config: Config = {}): Client => {
case 'arrayBuffer':
case 'blob':
case 'formData':
case 'json':
case 'text':
data = await response[parseAs]();
break;
case 'json': {
// Some servers return 200 with no Content-Length and empty body.
// response.json() would throw; read as text and parse if non-empty.
const text = await response.text();
data = text ? JSON.parse(text) : {};
break;
}
case 'stream':
return opts.responseStyle === 'data'
? response.body
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,16 @@ export const createClient = (config: Config = {}): Client => {
case 'arrayBuffer':
case 'blob':
case 'formData':
case 'json':
case 'text':
data = await response[parseAs]();
break;
case 'json': {
// Some servers return 200 with no Content-Length and empty body.
// response.json() would throw; read as text and parse if non-empty.
const text = await response.text();
data = text ? JSON.parse(text) : {};
break;
}
case 'stream':
return opts.responseStyle === 'data'
? response.body
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,16 @@ export const createClient = (config: Config = {}): Client => {
case 'arrayBuffer':
case 'blob':
case 'formData':
case 'json':
case 'text':
data = await response[parseAs]()
break
case 'json': {
// Some servers return 200 with no Content-Length and empty body.
// response.json() would throw; read as text and parse if non-empty.
const text = await response.text()
data = text ? JSON.parse(text) : {}
break
}
case 'stream':
return opts.responseStyle === 'data'
? response.body
Expand Down
65 changes: 64 additions & 1 deletion packages/custom-client/src/__tests__/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from 'vitest';
import { describe, expect, it, vi } from 'vitest';

import { createClient } from '../client';

Expand Down Expand Up @@ -48,3 +48,66 @@ describe('buildUrl', () => {
expect(client.buildUrl(options)).toBe(url);
});
});

describe('zero-length body handling', () => {
const client = createClient({ baseUrl: 'https://example.com' });

it('returns empty object for zero-length JSON response', async () => {
const mockResponse = new Response(null, {
headers: {
'Content-Length': '0',
'Content-Type': 'application/json',
},
status: 200,
});

const mockFetch = vi.fn().mockResolvedValue(mockResponse);

const result = await client.request({
fetch: mockFetch,
method: 'GET',
url: '/test',
});

expect(result.data).toEqual({});
});

it('returns empty object for empty JSON response without Content-Length header (status 200)', async () => {
// Simulates a server returning an empty body with status 200 and no Content-Length header
// This is the scenario described in the issue where response.json() throws
const mockResponse = new Response('', {
headers: {
'Content-Type': 'application/json',
},
status: 200,
});

const mockFetch = vi.fn().mockResolvedValue(mockResponse);

const result = await client.request({
fetch: mockFetch,
method: 'GET',
url: '/test',
});

expect(result.data).toEqual({});
});

it('returns empty object for empty response without Content-Length header and no Content-Type (defaults to JSON)', async () => {
// Tests the auto-detection behavior when no Content-Type is provided
const mockResponse = new Response('', {
status: 200,
});

const mockFetch = vi.fn().mockResolvedValue(mockResponse);

const result = await client.request({
fetch: mockFetch,
method: 'GET',
url: '/test',
});

// When parseAs is 'auto' and no Content-Type header exists, it should handle empty body gracefully
expect(result.data).toBeDefined();
});
});
8 changes: 7 additions & 1 deletion packages/custom-client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,16 @@ export const createClient = (config: Config = {}): Client => {
case 'arrayBuffer':
case 'blob':
case 'formData':
case 'json':
case 'text':
data = await response[parseAs]();
break;
case 'json': {
// Some servers return 200 with no Content-Length and empty body.
// response.json() would throw; read as text and parse if non-empty.
const text = await response.text();
data = text ? JSON.parse(text) : {};
break;
}
case 'stream':
return {
data: response.body,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,16 @@ export const createClient = (config: Config = {}): Client => {
case 'arrayBuffer':
case 'blob':
case 'formData':
case 'json':
case 'text':
data = await response[parseAs]();
break;
case 'json': {
// Some servers return 200 with no Content-Length and empty body.
// response.json() would throw; read as text and parse if non-empty.
const text = await response.text();
data = text ? JSON.parse(text) : {};
break;
}
case 'stream':
return opts.responseStyle === 'data'
? response.body
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,16 @@ export const createClient = (config: Config = {}): Client => {
case 'arrayBuffer':
case 'blob':
case 'formData':
case 'json':
case 'text':
data = await response[parseAs]();
break;
case 'json': {
// Some servers return 200 with no Content-Length and empty body.
// response.json() would throw; read as text and parse if non-empty.
const text = await response.text();
data = text ? JSON.parse(text) : {};
break;
}
case 'stream':
return opts.responseStyle === 'data'
? response.body
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,16 @@ export const createClient = (config: Config = {}): Client => {
case 'arrayBuffer':
case 'blob':
case 'formData':
case 'json':
case 'text':
data = await response[parseAs]();
break;
case 'json': {
// Some servers return 200 with no Content-Length and empty body.
// response.json() would throw; read as text and parse if non-empty.
const text = await response.text();
data = text ? JSON.parse(text) : {};
break;
}
case 'stream':
return opts.responseStyle === 'data'
? response.body
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,16 @@ export const createClient = (config: Config = {}): Client => {
case 'arrayBuffer':
case 'blob':
case 'formData':
case 'json':
case 'text':
data = await response[parseAs]();
break;
case 'json': {
// Some servers return 200 with no Content-Length and empty body.
// response.json() would throw; read as text and parse if non-empty.
const text = await response.text();
data = text ? JSON.parse(text) : {};
break;
}
case 'stream':
return opts.responseStyle === 'data'
? response.body
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,16 @@ export const createClient = (config: Config = {}): Client => {
case 'arrayBuffer':
case 'blob':
case 'formData':
case 'json':
case 'text':
data = await response[parseAs]();
break;
case 'json': {
// Some servers return 200 with no Content-Length and empty body.
// response.json() would throw; read as text and parse if non-empty.
const text = await response.text();
data = text ? JSON.parse(text) : {};
break;
}
case 'stream':
return opts.responseStyle === 'data'
? response.body
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,16 @@ export const createClient = (config: Config = {}): Client => {
case 'arrayBuffer':
case 'blob':
case 'formData':
case 'json':
case 'text':
data = await response[parseAs]();
break;
case 'json': {
// Some servers return 200 with no Content-Length and empty body.
// response.json() would throw; read as text and parse if non-empty.
const text = await response.text();
data = text ? JSON.parse(text) : {};
break;
}
case 'stream':
return opts.responseStyle === 'data'
? response.body
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,16 @@ export const createClient = (config: Config = {}): Client => {
case 'arrayBuffer':
case 'blob':
case 'formData':
case 'json':
case 'text':
data = await response[parseAs]();
break;
case 'json': {
// Some servers return 200 with no Content-Length and empty body.
// response.json() would throw; read as text and parse if non-empty.
const text = await response.text();
data = text ? JSON.parse(text) : {};
break;
}
case 'stream':
return opts.responseStyle === 'data'
? response.body
Expand Down
Loading
Loading