acacode / acacode/swagger-typescript-api

'multipart/form-data' formatter isn't recursive

未关闭
#462 4 条评论 4 个 reaction 已指派 0 人 在 GitHub 查看
bug
主要语言
TypeScript
星标
4.1k
派生
436
PR 合并指标
30 天内没有已合并 PR

描述

When using a `multipart/form-data` content type, the generated API code provides a private function to format certain types : `contentFormatters`.

Currently on version `12.0.2` the function is :

```ts
private contentFormatters: Record any> = {
[ContentType.Json]: (input: any) =>
input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
[ContentType.FormData]: (input: any) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
formData.append(
key,
property instanceof Blob
? property
: typeof property === "object" && property !== null
? JSON.stringify(property)
: `${property}`,
);
return formData;
}, new FormData()),
[ContentType.UrlEncoded]: (input: any) => this.toQueryString(input),
};
```

If we concentrate on the `ContentType.FormData` section of the formatter, we have

```ts
(input: any) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
formData.append(
key,
property instanceof Blob
? property
: typeof property === "object" && property !== null
? JSON.stringify(property)
: `${property}`,
);
return formData;
}, new FormData()),
```

This section is no recursive on objects.

For example, if my backend expects the following type for the payload

```ts
interface PayloadWithData {
items: {
name: string;
content: Blob;
appSpecificData: '1234567896',
}[];
}
```

The files aren't correctly set in the fetch request because they aren't serializable.

This should be updated to support File in subfields of the payload type.

EDIT 1 : Indentation
EDIT 2 : this looks like it's the continuation of the work done here : #293
EDIT 3 : Typo

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。