Skip to content

Commit ab522ef

Browse files
Send explicitly-provided empty object bodies
1 parent 795a33d commit ab522ef

19 files changed

Lines changed: 564 additions & 236 deletions

File tree

‎.changeset/early-bodies-stay.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@hey-api/openapi-ts": patch
3+
"@hey-api/custom-client": patch
4+
---
5+
6+
**client**: send an explicitly-provided empty object request body instead of dropping it
7+
8+
When an operation was called with an empty object body (for example when every body field is optional and none were supplied), `buildClientParams` treated the empty object as an unused slot and stripped it. The request was then sent with no body and no `Content-Type` header, which servers requiring `application/json` reject. An explicitly-provided body — including an empty object — is now preserved and sent as-is, while requests genuinely without a body continue to send none.

‎examples/openapi-ts-angular-common/src/client/core/params.gen.ts‎

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,32 +88,49 @@ function buildKeyMap(fields: FieldsConfig, map?: KeyMap): KeyMap {
8888
}
8989

9090
interface Params {
91-
body: unknown;
92-
headers: Record<string, unknown>;
93-
path: Record<string, unknown>;
94-
query: Record<string, unknown>;
91+
body?: unknown;
92+
headers?: Record<string, unknown>;
93+
path?: Record<string, unknown>;
94+
query?: Record<string, unknown>;
9595
}
9696

97-
type ParamsSlotMap = Record<Slot, unknown>;
98-
99-
function stripEmptySlots(params: ParamsSlotMap): void {
97+
// Strips constructed slots (headers/path/query) that ended up empty, so we
98+
// don't send an empty object the caller never populated. `body` is deliberately
99+
// excluded: unlike the other slots it is supplied by the caller, so an explicit
100+
// empty body (e.g. `{}`) is a real value and must be sent as-is.
101+
function stripEmptySlots(params: Params): void {
100102
for (const [slot, value] of Object.entries(params)) {
103+
if (slot === 'body') {
104+
continue;
105+
}
101106
if (value && typeof value === 'object' && !Array.isArray(value) && !Object.keys(value).length) {
102107
delete params[slot as Slot];
103108
}
104109
}
105110
}
106111

107112
export function buildClientParams(args: ReadonlyArray<unknown>, fields: FieldsConfig): Params {
108-
const params: ParamsSlotMap = {
109-
body: Object.create(null),
113+
const params: Params = {
110114
headers: Object.create(null),
111115
path: Object.create(null),
112116
query: Object.create(null),
113117
};
114118

115119
const map = buildKeyMap(fields);
116120

121+
// Writes a single field into its slot. `body` has no placeholder and is
122+
// created on first write, so a request without a body sends none, while an
123+
// explicitly-provided body is preserved by `stripEmptySlots`. The null
124+
// prototype guards against prototype pollution (GHSA-hhx9-57xq-r5rw).
125+
const writeSlot = (slot: Slot, key: string, value: unknown): void => {
126+
let record = params[slot] as Record<string, unknown> | undefined;
127+
if (record === undefined) {
128+
record = Object.create(null) as Record<string, unknown>;
129+
params[slot] = record;
130+
}
131+
record[key] = value;
132+
};
133+
117134
let config: FieldsConfig[number] | undefined;
118135

119136
for (const [index, arg] of args.entries()) {
@@ -130,7 +147,7 @@ export function buildClientParams(args: ReadonlyArray<unknown>, fields: FieldsCo
130147
const field = map.get(config.key)!;
131148
const name = field.map || config.key;
132149
if (field.in) {
133-
(params[field.in] as Record<string, unknown>)[name] = arg;
150+
writeSlot(field.in, name, arg);
134151
}
135152
} else {
136153
params.body = arg;
@@ -142,7 +159,7 @@ export function buildClientParams(args: ReadonlyArray<unknown>, fields: FieldsCo
142159
if (field) {
143160
if (field.in) {
144161
const name = field.map || key;
145-
(params[field.in] as Record<string, unknown>)[name] = value;
162+
writeSlot(field.in, name, value);
146163
} else {
147164
params[field.map] = value;
148165
}
@@ -151,11 +168,11 @@ export function buildClientParams(args: ReadonlyArray<unknown>, fields: FieldsCo
151168

152169
if (extra) {
153170
const [prefix, slot] = extra;
154-
(params[slot] as Record<string, unknown>)[key.slice(prefix.length)] = value;
171+
writeSlot(slot, key.slice(prefix.length), value);
155172
} else if ('allowExtra' in config && config.allowExtra) {
156173
for (const [slot, allowed] of Object.entries(config.allowExtra)) {
157174
if (allowed) {
158-
(params[slot as Slot] as Record<string, unknown>)[key] = value;
175+
writeSlot(slot as Slot, key, value);
159176
break;
160177
}
161178
}
@@ -167,5 +184,5 @@ export function buildClientParams(args: ReadonlyArray<unknown>, fields: FieldsCo
167184

168185
stripEmptySlots(params);
169186

170-
return params as Params;
187+
return params;
171188
}

‎examples/openapi-ts-angular/src/client/core/params.gen.ts‎

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,32 +88,49 @@ function buildKeyMap(fields: FieldsConfig, map?: KeyMap): KeyMap {
8888
}
8989

9090
interface Params {
91-
body: unknown;
92-
headers: Record<string, unknown>;
93-
path: Record<string, unknown>;
94-
query: Record<string, unknown>;
91+
body?: unknown;
92+
headers?: Record<string, unknown>;
93+
path?: Record<string, unknown>;
94+
query?: Record<string, unknown>;
9595
}
9696

97-
type ParamsSlotMap = Record<Slot, unknown>;
98-
99-
function stripEmptySlots(params: ParamsSlotMap): void {
97+
// Strips constructed slots (headers/path/query) that ended up empty, so we
98+
// don't send an empty object the caller never populated. `body` is deliberately
99+
// excluded: unlike the other slots it is supplied by the caller, so an explicit
100+
// empty body (e.g. `{}`) is a real value and must be sent as-is.
101+
function stripEmptySlots(params: Params): void {
100102
for (const [slot, value] of Object.entries(params)) {
103+
if (slot === 'body') {
104+
continue;
105+
}
101106
if (value && typeof value === 'object' && !Array.isArray(value) && !Object.keys(value).length) {
102107
delete params[slot as Slot];
103108
}
104109
}
105110
}
106111

107112
export function buildClientParams(args: ReadonlyArray<unknown>, fields: FieldsConfig): Params {
108-
const params: ParamsSlotMap = {
109-
body: Object.create(null),
113+
const params: Params = {
110114
headers: Object.create(null),
111115
path: Object.create(null),
112116
query: Object.create(null),
113117
};
114118

115119
const map = buildKeyMap(fields);
116120

121+
// Writes a single field into its slot. `body` has no placeholder and is
122+
// created on first write, so a request without a body sends none, while an
123+
// explicitly-provided body is preserved by `stripEmptySlots`. The null
124+
// prototype guards against prototype pollution (GHSA-hhx9-57xq-r5rw).
125+
const writeSlot = (slot: Slot, key: string, value: unknown): void => {
126+
let record = params[slot] as Record<string, unknown> | undefined;
127+
if (record === undefined) {
128+
record = Object.create(null) as Record<string, unknown>;
129+
params[slot] = record;
130+
}
131+
record[key] = value;
132+
};
133+
117134
let config: FieldsConfig[number] | undefined;
118135

119136
for (const [index, arg] of args.entries()) {
@@ -130,7 +147,7 @@ export function buildClientParams(args: ReadonlyArray<unknown>, fields: FieldsCo
130147
const field = map.get(config.key)!;
131148
const name = field.map || config.key;
132149
if (field.in) {
133-
(params[field.in] as Record<string, unknown>)[name] = arg;
150+
writeSlot(field.in, name, arg);
134151
}
135152
} else {
136153
params.body = arg;
@@ -142,7 +159,7 @@ export function buildClientParams(args: ReadonlyArray<unknown>, fields: FieldsCo
142159
if (field) {
143160
if (field.in) {
144161
const name = field.map || key;
145-
(params[field.in] as Record<string, unknown>)[name] = value;
162+
writeSlot(field.in, name, value);
146163
} else {
147164
params[field.map] = value;
148165
}
@@ -151,11 +168,11 @@ export function buildClientParams(args: ReadonlyArray<unknown>, fields: FieldsCo
151168

152169
if (extra) {
153170
const [prefix, slot] = extra;
154-
(params[slot] as Record<string, unknown>)[key.slice(prefix.length)] = value;
171+
writeSlot(slot, key.slice(prefix.length), value);
155172
} else if ('allowExtra' in config && config.allowExtra) {
156173
for (const [slot, allowed] of Object.entries(config.allowExtra)) {
157174
if (allowed) {
158-
(params[slot as Slot] as Record<string, unknown>)[key] = value;
175+
writeSlot(slot as Slot, key, value);
159176
break;
160177
}
161178
}
@@ -167,5 +184,5 @@ export function buildClientParams(args: ReadonlyArray<unknown>, fields: FieldsCo
167184

168185
stripEmptySlots(params);
169186

170-
return params as Params;
187+
return params;
171188
}

‎examples/openapi-ts-axios/src/client/core/params.gen.ts‎

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,32 +88,49 @@ function buildKeyMap(fields: FieldsConfig, map?: KeyMap): KeyMap {
8888
}
8989

9090
interface Params {
91-
body: unknown;
92-
headers: Record<string, unknown>;
93-
path: Record<string, unknown>;
94-
query: Record<string, unknown>;
91+
body?: unknown;
92+
headers?: Record<string, unknown>;
93+
path?: Record<string, unknown>;
94+
query?: Record<string, unknown>;
9595
}
9696

97-
type ParamsSlotMap = Record<Slot, unknown>;
98-
99-
function stripEmptySlots(params: ParamsSlotMap): void {
97+
// Strips constructed slots (headers/path/query) that ended up empty, so we
98+
// don't send an empty object the caller never populated. `body` is deliberately
99+
// excluded: unlike the other slots it is supplied by the caller, so an explicit
100+
// empty body (e.g. `{}`) is a real value and must be sent as-is.
101+
function stripEmptySlots(params: Params): void {
100102
for (const [slot, value] of Object.entries(params)) {
103+
if (slot === 'body') {
104+
continue;
105+
}
101106
if (value && typeof value === 'object' && !Array.isArray(value) && !Object.keys(value).length) {
102107
delete params[slot as Slot];
103108
}
104109
}
105110
}
106111

107112
export function buildClientParams(args: ReadonlyArray<unknown>, fields: FieldsConfig): Params {
108-
const params: ParamsSlotMap = {
109-
body: Object.create(null),
113+
const params: Params = {
110114
headers: Object.create(null),
111115
path: Object.create(null),
112116
query: Object.create(null),
113117
};
114118

115119
const map = buildKeyMap(fields);
116120

121+
// Writes a single field into its slot. `body` has no placeholder and is
122+
// created on first write, so a request without a body sends none, while an
123+
// explicitly-provided body is preserved by `stripEmptySlots`. The null
124+
// prototype guards against prototype pollution (GHSA-hhx9-57xq-r5rw).
125+
const writeSlot = (slot: Slot, key: string, value: unknown): void => {
126+
let record = params[slot] as Record<string, unknown> | undefined;
127+
if (record === undefined) {
128+
record = Object.create(null) as Record<string, unknown>;
129+
params[slot] = record;
130+
}
131+
record[key] = value;
132+
};
133+
117134
let config: FieldsConfig[number] | undefined;
118135

119136
for (const [index, arg] of args.entries()) {
@@ -130,7 +147,7 @@ export function buildClientParams(args: ReadonlyArray<unknown>, fields: FieldsCo
130147
const field = map.get(config.key)!;
131148
const name = field.map || config.key;
132149
if (field.in) {
133-
(params[field.in] as Record<string, unknown>)[name] = arg;
150+
writeSlot(field.in, name, arg);
134151
}
135152
} else {
136153
params.body = arg;
@@ -142,7 +159,7 @@ export function buildClientParams(args: ReadonlyArray<unknown>, fields: FieldsCo
142159
if (field) {
143160
if (field.in) {
144161
const name = field.map || key;
145-
(params[field.in] as Record<string, unknown>)[name] = value;
162+
writeSlot(field.in, name, value);
146163
} else {
147164
params[field.map] = value;
148165
}
@@ -151,11 +168,11 @@ export function buildClientParams(args: ReadonlyArray<unknown>, fields: FieldsCo
151168

152169
if (extra) {
153170
const [prefix, slot] = extra;
154-
(params[slot] as Record<string, unknown>)[key.slice(prefix.length)] = value;
171+
writeSlot(slot, key.slice(prefix.length), value);
155172
} else if ('allowExtra' in config && config.allowExtra) {
156173
for (const [slot, allowed] of Object.entries(config.allowExtra)) {
157174
if (allowed) {
158-
(params[slot as Slot] as Record<string, unknown>)[key] = value;
175+
writeSlot(slot as Slot, key, value);
159176
break;
160177
}
161178
}
@@ -167,5 +184,5 @@ export function buildClientParams(args: ReadonlyArray<unknown>, fields: FieldsCo
167184

168185
stripEmptySlots(params);
169186

170-
return params as Params;
187+
return params;
171188
}

0 commit comments

Comments
 (0)