Skip to content

Commit

Permalink
fix(form-data): handling array items
Browse files Browse the repository at this point in the history
  • Loading branch information
BijanRegmi committed Apr 29, 2024
1 parent fb32cf5 commit d25c506
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions packages/core/src/getters/res-req-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SchemaObject>(
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 (
Expand Down Expand Up @@ -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<SchemaObject>(
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' ||
Expand Down

0 comments on commit d25c506

Please sign in to comment.