Wrong code gets generated with array of formData fields
See original GitHub issueWhat are the steps to reproduce this issue?
- Generate code with a swagger.json that has a formData field of an array type
"parameters": [
{
"type": "array",
"items": {
"type": "string"
},
"description": "Image to upload",
"name": "image",
"in": "formData",
"required": true
}
]
What happens?
The following code gets generated
export const meAvatarUpload = <TData = WebMediaUploadResponse[]>(
meAvatarUploadBody: MeAvatarUploadBody,
options?: SecondParameter<typeof customInstance>) => {const formData = new FormData();
formData.append('image', JSON.stringify(meAvatarUploadBody.image)
// ^ this is wrong, both because of the JSON.stringify() approach and because of the missing closing paren.
return customInstance<TData>(
{url: `/me/media`, method: 'post',
data: formData
},
// eslint-disable-next-line
// @ts-ignore
options);
}
What were you expecting to happen?
The following code to be generated instead
export const meAvatarUpload = <TData = WebMediaUploadResponse[]>(
meAvatarUploadBody: MeAvatarUploadBody,
options?: SecondParameter<typeof customInstance>) => {const formData = new FormData();
meAvatarUploadBody.image.forEach(image => formData.append('image', image))
// Manually editing the code makes it work
return customInstance<TData>(
{url: `/me/media`, method: 'post',
data: formData
},
// eslint-disable-next-line
// @ts-ignore
options);
}
What versions are you using?
Operating System: macOS 11.5 Package Version: 5.5.2 Browser Version: 92.0.4515.107
Issue Analytics
- State:
- Created 2 years ago
- Comments:16 (15 by maintainers)
Top Results From Across the Web
Can I append an array to 'formdata' in javascript?
The way I see it, your tags array contains objects (@Musa is right, btw; making this_tag an Array, then assigning string properties to...
Read more >FormData - The Modern JavaScript Tutorial
This chapter is about sending HTML forms: with or without files, with additional fields and so on. FormData objects can help with that....
Read more >http_build_query - Manual - PHP
Generates a URL-encoded query string from the associative (or indexed) array provided. Parameters ¶. data. May be an array or object containing properties....
Read more >10 Most Common Mistakes That PHP Developers Make - Toptal
The issue is that the above code confuses returning arrays by reference with returning arrays by value. Unless you explicitly tell PHP to...
Read more >Using XMLHttpRequest - Web APIs | MDN
However, this method is a "last resort" since if the XML code changes ... Using the FormData API is the simplest and fastest,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
fixed with
6.2.4
In version 6, I will add a way to override the default generation (I am currently working on it on the next branch)