openapi-typescript-codegen icon indicating copy to clipboard operation
openapi-typescript-codegen copied to clipboard

fix: Object.entries(formData) in getFormData

Open dmitriyzhukcoso opened this issue 1 year ago • 4 comments

When you use Object.entries(formData), it attempts to convert the FormData object into an array of key-value pairs, as it would with a regular JavaScript object. However, since FormData doesn't store its data in enumerable properties, Object.entries returns an empty array.

This will not work:

Object.entries(options.formData) 
        .filter(([_, value]) => isDefined(value))
        .forEach(([key, value]) => {
            if (Array.isArray(value)) {
                value.forEach(v => process(key, v));
            } else {
                process(key, value);
            }
        });

On the other hand, formData.entries() is a method specifically provided by the FormData interface. It returns an iterator allowing for the traversal of all key/value pairs contained in the FormData object. This method is designed to understand and interact with the internal structure of FormData, so it successfully retrieves the data.

This will work:

for (let [key, value] of options.formData.entries()) {
      if (isDefined(value)) {
        if (Array.isArray(value)) {
          value.forEach((v) => process(key, v));
        } else {
          process(key, value);
        }
      }
    }

dmitriyzhukcoso avatar Nov 17 '23 20:11 dmitriyzhukcoso

Hi, will this be added any time soon?

dmitriyzhuk avatar Dec 18 '23 20:12 dmitriyzhuk

+1, waiting for this fix as well

stasdrvn avatar Dec 18 '23 20:12 stasdrvn

+1

illiaDream avatar Dec 19 '23 07:12 illiaDream

Check out our fork of this repository @hey-api/openapi-ts. We have fixed this issue in v0.32.1. If you run into any further issues, open an issue in our repository. Thanks.

jordanshatford avatar Mar 30 '24 23:03 jordanshatford