question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong code gets generated with array of formData fields

See original GitHub issue

What are the steps to reproduce this issue?

  1. 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:closed
  • Created 2 years ago
  • Comments:16 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
anymaniaxcommented, Oct 28, 2021

fixed with 6.2.4

1reaction
anymaniaxcommented, Aug 2, 2021

In version 6, I will add a way to override the default generation (I am currently working on it on the next branch)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found