Skip to content

Commit 01b8086

Browse files
authored
Merge pull request #2564 from franworks/fix/#2553-handle-plain-text-request-body
2 parents 62cff55 + f653716 commit 01b8086

File tree

147 files changed

+3788
-455
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

147 files changed

+3788
-455
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hey-api/openapi-ts": patch
3+
---
4+
5+
fix(client): improve handling of plain text, falsy, and unserialized request bodies

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/body-response-text-plain/client/client.gen.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ export const createClient = (config: Config = {}): Client => {
6060
await opts.requestValidator(opts);
6161
}
6262

63-
if (opts.body && opts.bodySerializer) {
63+
if (opts.body !== undefined && opts.bodySerializer) {
6464
opts.serializedBody = opts.bodySerializer(opts.body);
6565
}
6666

6767
// remove Content-Type header if body is empty to avoid sending invalid requests
68-
if (opts.serializedBody === undefined || opts.serializedBody === '') {
68+
if (opts.body === undefined || opts.serializedBody === '') {
6969
opts.headers.delete('Content-Type');
7070
}
7171

@@ -80,7 +80,7 @@ export const createClient = (config: Config = {}): Client => {
8080
const requestInit: ReqInit = {
8181
redirect: 'follow',
8282
...opts,
83-
body: opts.serializedBody,
83+
body: getValidRequestBody(opts),
8484
};
8585

8686
let request = new Request(url, requestInit);
@@ -212,6 +212,26 @@ export const createClient = (config: Config = {}): Client => {
212212
};
213213
};
214214

215+
function getValidRequestBody(options: ResolvedRequestOptions) {
216+
const hasBody = options.body !== undefined;
217+
const isSerializedBody = hasBody && options.bodySerializer;
218+
219+
if (isSerializedBody) {
220+
const hasSerializedBody =
221+
options.serializedBody !== undefined && options.serializedBody !== '';
222+
223+
return hasSerializedBody ? options.serializedBody : null;
224+
}
225+
226+
// plain/text body
227+
if (hasBody) {
228+
return options.body;
229+
}
230+
231+
// no body was provided
232+
return undefined;
233+
}
234+
215235
const makeMethodFn =
216236
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
217237
request({ ...options, method });

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/form-data/client/client.gen.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ export const createClient = (config: Config = {}): Client => {
6060
await opts.requestValidator(opts);
6161
}
6262

63-
if (opts.body && opts.bodySerializer) {
63+
if (opts.body !== undefined && opts.bodySerializer) {
6464
opts.serializedBody = opts.bodySerializer(opts.body);
6565
}
6666

6767
// remove Content-Type header if body is empty to avoid sending invalid requests
68-
if (opts.serializedBody === undefined || opts.serializedBody === '') {
68+
if (opts.body === undefined || opts.serializedBody === '') {
6969
opts.headers.delete('Content-Type');
7070
}
7171

@@ -80,7 +80,7 @@ export const createClient = (config: Config = {}): Client => {
8080
const requestInit: ReqInit = {
8181
redirect: 'follow',
8282
...opts,
83-
body: opts.serializedBody,
83+
body: getValidRequestBody(opts),
8484
};
8585

8686
let request = new Request(url, requestInit);
@@ -212,6 +212,26 @@ export const createClient = (config: Config = {}): Client => {
212212
};
213213
};
214214

215+
function getValidRequestBody(options: ResolvedRequestOptions) {
216+
const hasBody = options.body !== undefined;
217+
const isSerializedBody = hasBody && options.bodySerializer;
218+
219+
if (isSerializedBody) {
220+
const hasSerializedBody =
221+
options.serializedBody !== undefined && options.serializedBody !== '';
222+
223+
return hasSerializedBody ? options.serializedBody : null;
224+
}
225+
226+
// plain/text body
227+
if (hasBody) {
228+
return options.body;
229+
}
230+
231+
// no body was provided
232+
return undefined;
233+
}
234+
215235
const makeMethodFn =
216236
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
217237
request({ ...options, method });

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/plugins/@hey-api/client-fetch/sdk-nested-classes-instance/client/client.gen.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ export const createClient = (config: Config = {}): Client => {
6060
await opts.requestValidator(opts);
6161
}
6262

63-
if (opts.body && opts.bodySerializer) {
63+
if (opts.body !== undefined && opts.bodySerializer) {
6464
opts.serializedBody = opts.bodySerializer(opts.body);
6565
}
6666

6767
// remove Content-Type header if body is empty to avoid sending invalid requests
68-
if (opts.serializedBody === undefined || opts.serializedBody === '') {
68+
if (opts.body === undefined || opts.serializedBody === '') {
6969
opts.headers.delete('Content-Type');
7070
}
7171

@@ -80,7 +80,7 @@ export const createClient = (config: Config = {}): Client => {
8080
const requestInit: ReqInit = {
8181
redirect: 'follow',
8282
...opts,
83-
body: opts.serializedBody,
83+
body: getValidRequestBody(opts),
8484
};
8585

8686
let request = new Request(url, requestInit);
@@ -212,6 +212,26 @@ export const createClient = (config: Config = {}): Client => {
212212
};
213213
};
214214

215+
function getValidRequestBody(options: ResolvedRequestOptions) {
216+
const hasBody = options.body !== undefined;
217+
const isSerializedBody = hasBody && options.bodySerializer;
218+
219+
if (isSerializedBody) {
220+
const hasSerializedBody =
221+
options.serializedBody !== undefined && options.serializedBody !== '';
222+
223+
return hasSerializedBody ? options.serializedBody : null;
224+
}
225+
226+
// plain/text body
227+
if (hasBody) {
228+
return options.body;
229+
}
230+
231+
// no body was provided
232+
return undefined;
233+
}
234+
215235
const makeMethodFn =
216236
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
217237
request({ ...options, method });

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/plugins/@hey-api/client-fetch/sdk-nested-classes/client/client.gen.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ export const createClient = (config: Config = {}): Client => {
6060
await opts.requestValidator(opts);
6161
}
6262

63-
if (opts.body && opts.bodySerializer) {
63+
if (opts.body !== undefined && opts.bodySerializer) {
6464
opts.serializedBody = opts.bodySerializer(opts.body);
6565
}
6666

6767
// remove Content-Type header if body is empty to avoid sending invalid requests
68-
if (opts.serializedBody === undefined || opts.serializedBody === '') {
68+
if (opts.body === undefined || opts.serializedBody === '') {
6969
opts.headers.delete('Content-Type');
7070
}
7171

@@ -80,7 +80,7 @@ export const createClient = (config: Config = {}): Client => {
8080
const requestInit: ReqInit = {
8181
redirect: 'follow',
8282
...opts,
83-
body: opts.serializedBody,
83+
body: getValidRequestBody(opts),
8484
};
8585

8686
let request = new Request(url, requestInit);
@@ -212,6 +212,26 @@ export const createClient = (config: Config = {}): Client => {
212212
};
213213
};
214214

215+
function getValidRequestBody(options: ResolvedRequestOptions) {
216+
const hasBody = options.body !== undefined;
217+
const isSerializedBody = hasBody && options.bodySerializer;
218+
219+
if (isSerializedBody) {
220+
const hasSerializedBody =
221+
options.serializedBody !== undefined && options.serializedBody !== '';
222+
223+
return hasSerializedBody ? options.serializedBody : null;
224+
}
225+
226+
// plain/text body
227+
if (hasBody) {
228+
return options.body;
229+
}
230+
231+
// no body was provided
232+
return undefined;
233+
}
234+
215235
const makeMethodFn =
216236
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
217237
request({ ...options, method });

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/plugins/@hey-api/sdk/default/client/client.gen.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ export const createClient = (config: Config = {}): Client => {
6060
await opts.requestValidator(opts);
6161
}
6262

63-
if (opts.body && opts.bodySerializer) {
63+
if (opts.body !== undefined && opts.bodySerializer) {
6464
opts.serializedBody = opts.bodySerializer(opts.body);
6565
}
6666

6767
// remove Content-Type header if body is empty to avoid sending invalid requests
68-
if (opts.serializedBody === undefined || opts.serializedBody === '') {
68+
if (opts.body === undefined || opts.serializedBody === '') {
6969
opts.headers.delete('Content-Type');
7070
}
7171

@@ -80,7 +80,7 @@ export const createClient = (config: Config = {}): Client => {
8080
const requestInit: ReqInit = {
8181
redirect: 'follow',
8282
...opts,
83-
body: opts.serializedBody,
83+
body: getValidRequestBody(opts),
8484
};
8585

8686
let request = new Request(url, requestInit);
@@ -212,6 +212,26 @@ export const createClient = (config: Config = {}): Client => {
212212
};
213213
};
214214

215+
function getValidRequestBody(options: ResolvedRequestOptions) {
216+
const hasBody = options.body !== undefined;
217+
const isSerializedBody = hasBody && options.bodySerializer;
218+
219+
if (isSerializedBody) {
220+
const hasSerializedBody =
221+
options.serializedBody !== undefined && options.serializedBody !== '';
222+
223+
return hasSerializedBody ? options.serializedBody : null;
224+
}
225+
226+
// plain/text body
227+
if (hasBody) {
228+
return options.body;
229+
}
230+
231+
// no body was provided
232+
return undefined;
233+
}
234+
215235
const makeMethodFn =
216236
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
217237
request({ ...options, method });

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/plugins/@hey-api/sdk/instance/client/client.gen.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ export const createClient = (config: Config = {}): Client => {
6060
await opts.requestValidator(opts);
6161
}
6262

63-
if (opts.body && opts.bodySerializer) {
63+
if (opts.body !== undefined && opts.bodySerializer) {
6464
opts.serializedBody = opts.bodySerializer(opts.body);
6565
}
6666

6767
// remove Content-Type header if body is empty to avoid sending invalid requests
68-
if (opts.serializedBody === undefined || opts.serializedBody === '') {
68+
if (opts.body === undefined || opts.serializedBody === '') {
6969
opts.headers.delete('Content-Type');
7070
}
7171

@@ -80,7 +80,7 @@ export const createClient = (config: Config = {}): Client => {
8080
const requestInit: ReqInit = {
8181
redirect: 'follow',
8282
...opts,
83-
body: opts.serializedBody,
83+
body: getValidRequestBody(opts),
8484
};
8585

8686
let request = new Request(url, requestInit);
@@ -212,6 +212,26 @@ export const createClient = (config: Config = {}): Client => {
212212
};
213213
};
214214

215+
function getValidRequestBody(options: ResolvedRequestOptions) {
216+
const hasBody = options.body !== undefined;
217+
const isSerializedBody = hasBody && options.bodySerializer;
218+
219+
if (isSerializedBody) {
220+
const hasSerializedBody =
221+
options.serializedBody !== undefined && options.serializedBody !== '';
222+
223+
return hasSerializedBody ? options.serializedBody : null;
224+
}
225+
226+
// plain/text body
227+
if (hasBody) {
228+
return options.body;
229+
}
230+
231+
// no body was provided
232+
return undefined;
233+
}
234+
215235
const makeMethodFn =
216236
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
217237
request({ ...options, method });

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/plugins/@hey-api/sdk/throwOnError/client/client.gen.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ export const createClient = (config: Config = {}): Client => {
6060
await opts.requestValidator(opts);
6161
}
6262

63-
if (opts.body && opts.bodySerializer) {
63+
if (opts.body !== undefined && opts.bodySerializer) {
6464
opts.serializedBody = opts.bodySerializer(opts.body);
6565
}
6666

6767
// remove Content-Type header if body is empty to avoid sending invalid requests
68-
if (opts.serializedBody === undefined || opts.serializedBody === '') {
68+
if (opts.body === undefined || opts.serializedBody === '') {
6969
opts.headers.delete('Content-Type');
7070
}
7171

@@ -80,7 +80,7 @@ export const createClient = (config: Config = {}): Client => {
8080
const requestInit: ReqInit = {
8181
redirect: 'follow',
8282
...opts,
83-
body: opts.serializedBody,
83+
body: getValidRequestBody(opts),
8484
};
8585

8686
let request = new Request(url, requestInit);
@@ -212,6 +212,26 @@ export const createClient = (config: Config = {}): Client => {
212212
};
213213
};
214214

215+
function getValidRequestBody(options: ResolvedRequestOptions) {
216+
const hasBody = options.body !== undefined;
217+
const isSerializedBody = hasBody && options.bodySerializer;
218+
219+
if (isSerializedBody) {
220+
const hasSerializedBody =
221+
options.serializedBody !== undefined && options.serializedBody !== '';
222+
223+
return hasSerializedBody ? options.serializedBody : null;
224+
}
225+
226+
// plain/text body
227+
if (hasBody) {
228+
return options.body;
229+
}
230+
231+
// no body was provided
232+
return undefined;
233+
}
234+
215235
const makeMethodFn =
216236
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
217237
request({ ...options, method });

0 commit comments

Comments
 (0)