Skip to content

Commit

Permalink
fix(form-data): handling array items (orval-labs#1336)
Browse files Browse the repository at this point in the history
  • Loading branch information
BijanRegmi authored Jul 19, 2024
1 parent c70fe7b commit 238dfa4
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 @@ -342,7 +342,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 @@ -390,7 +407,23 @@ const resolveSchemaPropertiesToFormData = ({
if (property.type === 'object') {
formDataValue = `${variableName}.append('${key}', JSON.stringify(${nonOptionalValueKey}));\n`;
} else if (property.type === 'array') {
formDataValue = `${nonOptionalValueKey}.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 238dfa4

Please sign in to comment.