Skip to content

Fixed a bug which caused snippets generation to fail for form data params with no type key present#252

Merged
shreys7 merged 3 commits intodevelopfrom
feauture/fix-type-absent-formparams
May 21, 2020
Merged

Fixed a bug which caused snippets generation to fail for form data params with no type key present#252
shreys7 merged 3 commits intodevelopfrom
feauture/fix-type-absent-formparams

Conversation

@shreys7
Copy link
Member

@shreys7 shreys7 commented May 20, 2020

Affected codegens

  • js-jquery
  • powershell-restmethod

RCA

  • The code generator throws the following error:
TypeError: Cannot read property 'split' of undefined
  • We use src.split to get the filename from the path of the file stored in formParam.src field
  • This problem is seen in older collection versions, where the type parameter is not present in form-data parameters. The way we generate code for form params is as following:
if (value.type === 'text') {
       return (`form.append("${sanitize(value.key, request.body.mode, trimRequestBody)}", "` +
                   `${sanitize(value.value, request.body.mode, trimRequestBody)}");`);
     }
     var pathArray = value.src.split(path.sep),
       fileName = pathArray[pathArray.length - 1];
     return (`form.append("${sanitize(value.key, request.body.mode, trimRequestBody)}", fileInput.files[0], ` +
                   `"${sanitize(fileName, request.body.mode, trimRequestBody)}");`);
   });
 form += `${formMap.join('\n')}\n\n`;
}
  • The form parameter under consideration is of type text, but due to the older collection version, the parameter is missing a type property. This property is present in v2.1 of collections and is set to text and file for text and file parameters respectively.
  • The form parameter does not have a value.type property and hence the code assumes that the current form parameter is a type=file parameter. Hence we try to get the file name from the src field which has the path of the file stored in it. This throws an error as value.src is undefined

Why are the other code generators that access the value.src for getting file name not failing?

  • Well, in all the other code generators, we check for the type=file before, and if the type is not a file, default to it being a text parameter. The code for the same is shown below:
if (data.type === 'file') {
  var pathArray = data.src.split(path.sep),
   fileName = pathArray[pathArray.length - 1];
  bodySnippet += `formdata.append("${sanitize(data.key, trim)}", fileInput.files[0], "${fileName}");\n`;
}
else {
  bodySnippet += `formdata.append("${sanitize(data.key, trim)}", "${sanitize(data.value, trim)}");\n`;
}
  • Thus, we check for param.src if and only if type is defined as file. For all the other cases, consider it as a text parameter and just put the text as provided by the value field from form-data param in the code generated

@shreys7 shreys7 force-pushed the feauture/fix-type-absent-formparams branch from 722efc4 to 47426e8 Compare May 21, 2020 11:13
@shreys7 shreys7 merged commit 1aeaa79 into develop May 21, 2020
@umeshp7 umeshp7 deleted the feauture/fix-type-absent-formparams branch October 9, 2020 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants