Skip to content

Commit c2d3e6a

Browse files
frazarsmorimoto
andauthored
Fix handling of input of type FormData for the Fetch client (#1008)
* Skip formatting if FormData input is provided * Update all unit tests snapshots * Changeset Signed-off-by: Sora Morimoto <[email protected]> --------- Signed-off-by: Sora Morimoto <[email protected]> Co-authored-by: Sora Morimoto <[email protected]>
1 parent c604d1f commit c2d3e6a

File tree

29 files changed

+898
-330
lines changed

29 files changed

+898
-330
lines changed

.changeset/loud-dancers-shake.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
"swagger-typescript-api": patch
3+
---
4+
5+
Fix handling of FormData inputs in Fetch HTTP client
6+
7+
Previously, when users passed a `FormData` object directly to the Fetch
8+
client's `multipart/form-data` formatter, it would incorrectly attempt to use
9+
`Object.keys()` on the FormData instance, which returns an empty array. This
10+
caused the FormData to be processed incorrectly.
11+
12+
The fix adds a type check to return FormData instances unchanged, allowing
13+
users to have full control over FormData construction when needed whilst
14+
maintaining backwards compatibility for object inputs. This aligns the Fetch
15+
client behaviour with the existing Axios client implementation.
16+
17+
This resolves issues where users needed to send multipart requests with
18+
multiple entries for the same key, which is only possible with direct FormData
19+
manipulation.

templates/base/http-clients/fetch-http-client.ejs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,12 @@ export class HttpClient<SecurityDataType = unknown> {
106106
[ContentType.Json]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
107107
[ContentType.JsonApi]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
108108
[ContentType.Text]: (input:any) => input !== null && typeof input !== "string" ? JSON.stringify(input) : input,
109-
[ContentType.FormData]: (input: any) =>
110-
Object.keys(input || {}).reduce((formData, key) => {
109+
[ContentType.FormData]: (input: any) => {
110+
if (input instanceof FormData) {
111+
return input;
112+
}
113+
114+
return Object.keys(input || {}).reduce((formData, key) => {
111115
const property = input[key];
112116
formData.append(
113117
key,
@@ -118,7 +122,8 @@ export class HttpClient<SecurityDataType = unknown> {
118122
`${property}`
119123
);
120124
return formData;
121-
}, new FormData()),
125+
}, new FormData());
126+
},
122127
[ContentType.UrlEncoded]: (input: any) => this.toQueryString(input),
123128
}
124129

0 commit comments

Comments
 (0)