From d25c506519cc232110a8808fb2a9e838d312543c Mon Sep 17 00:00:00 2001 From: "(void *)0x0" <55084653+BijanRegmi@users.noreply.github.com> Date: Mon, 29 Apr 2024 21:56:54 +0545 Subject: [PATCH] fix(form-data): handling array items --- packages/core/src/getters/res-req-types.ts | 37 ++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/packages/core/src/getters/res-req-types.ts b/packages/core/src/getters/res-req-types.ts index b1ad6d077..754a6cb4b 100644 --- a/packages/core/src/getters/res-req-types.ts +++ b/packages/core/src/getters/res-req-types.ts @@ -284,7 +284,24 @@ const getSchemaFormDataAndUrlEncoded = ({ } if (schema.type === 'array') { - return `${form}${propName}.forEach(value => ${variableName}.append('data', value))\n`; + let valueStr = 'value'; + if (schema.items) { + const { schema: itemSchema } = resolveRef( + schema.items, + context, + ); + if (itemSchema.type === 'object' || itemSchema.type === 'array') { + valueStr = 'JSON.stringify(value)'; + } else if ( + itemSchema.type === 'number' || + itemSchema.type === 'integer' || + itemSchema.type === 'boolean' + ) { + valueStr = 'value.toString()'; + } + } + + return `${form}${propName}.forEach(value => ${variableName}.append('data', ${valueStr}))\n`; } if ( @@ -324,7 +341,23 @@ const resolveSchemaPropertiesToFormData = ({ if (property.type === 'object') { formDataValue = `${variableName}.append('${key}', JSON.stringify(${valueKey}));\n`; } else if (property.type === 'array') { - formDataValue = `${valueKey}.forEach(value => ${variableName}.append('${key}', value));\n`; + let valueStr = 'value'; + if (property.items) { + const { schema: itemSchema } = resolveRef( + property.items, + context, + ); + if (itemSchema.type === 'object' || itemSchema.type === 'array') { + valueStr = 'JSON.stringify(value)'; + } else if ( + itemSchema.type === 'number' || + itemSchema.type === 'integer' || + itemSchema.type === 'boolean' + ) { + valueStr = 'value.toString()'; + } + } + formDataValue = `${valueKey}.forEach(value => ${variableName}.append('${key}', ${valueStr}));\n`; } else if ( property.type === 'number' || property.type === 'integer' ||