-
-
Notifications
You must be signed in to change notification settings - Fork 414
fix: handle arrays properly in FormData #1431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
When FormData contains arrays (e.g., multiple file uploads), the current implementation incorrectly stringifies the entire array instead of appending each item individually. This fix: - Detects when a property is an array - Iterates through each array item - Properly appends each file/blob individually to FormData - Preserves non-file array items by JSON.stringify-ing them individually This enables proper multi-file uploads and array handling in FormData, which is essential for APIs that accept multiple attachments or files.
|
|
||
for (const formItem of propertyContent) { | ||
const isFileType = formItem instanceof Blob || formItem instanceof File; | ||
formData.append(key, isFileType ? formItem : JSON.stringify(formItem)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const propertyContent = property instanceof Array ? property : [property]; | ||
|
||
for (const formItem of propertyContent) { | ||
const isFileType = formItem instanceof Blob || formItem instanceof File; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since File extends Blob, you can simplify this check to just formItem instanceof Blob
.
- Remove redundant File instanceof check (File extends Blob) - Add explicit string type check to prevent double-stringification - Strings now passed directly to FormData instead of JSON.stringify Thanks to @lc-soft for the code review and suggestions! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
@lc-soft Thank you for the excellent code review! I've implemented both of your suggestions:
The updated code now correctly handles:
Most importantly: The original bug fix (handling arrays in FormData for multi-file uploads) still works perfectly! 🎉 Changes pushed in commit b9da1cd |
Update test snapshots to reflect the simplified FormData handling: - Removed redundant File instanceof check - Added string type check to prevent double-stringification All 133 tests passing. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
✅ Tests fixed! Updated all test snapshots to reflect the refactored FormData handling. All 133 tests now passing. Changes in commit 6e854b8:
|
Description
This PR fixes an issue with FormData handling when arrays are involved, particularly for multi-file uploads.
The Problem
When an API endpoint accepts an array of files (or any array in FormData), the current implementation incorrectly converts the entire array to a string representation instead of properly appending each item individually to the FormData object.
Example scenario:
The Solution
This fix modifies the FormData content formatter to:
Changes Made
templates/base/http-clients/fetch-http-client.ejs
to handle arrays in FormDataformdata-arrays.json
to verify the fixUse Cases
This fix is essential for:
Testing
Backwards Compatibility
This change is fully backwards compatible: