Skip to content
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

Introduce formDataArrayFormat option #1901

Open
wants to merge 1 commit into
base: v1
Choose a base branch
from

Conversation

skryukov
Copy link
Contributor

@skryukov skryukov commented Jun 21, 2024

This PR adds a new formDataArrayFormat option to Link and router.visit that duplicates the logic of queryStringArrayFormat, but for formData arrays serialization.

Rationale

Currently, multipart form data arrays are always serialized using indices:

------WebKitFormBoundarynVnO6UtO8ts1Qa5q
Content-Disposition: form-data; name="model[files][0]"; filename="aliens.png"
Content-Type: image/png


------WebKitFormBoundarynVnO6UtO8ts1Qa5q
Content-Disposition: form-data; name="model[files][1]"; filename="ufo.jpg"
Content-Type: image/jpeg

Rails (or more precisely Rack) parses this as hashes with keys '0' and '1'. To support scenarios like a file input with the multiple attribute, we need to allow the tweaking of this behavior.

This PR introduces the formDataArrayFormat option to router.visit. With this option set to 'brackets', we serialize the keys in the format required by Rack/Rails:

  form.post(`/models`, {
    formDataArrayFormat: 'brackets',
  })
------WebKitFormBoundaryxrZa60F5B7gxV5aG
Content-Disposition: form-data; name="model[files][]"; filename="aliens.png"
Content-Type: image/png


------WebKitFormBoundaryxrZa60F5B7gxV5aG
Content-Disposition: form-data; name="model[files][]"; filename="ufo.jpg"
Content-Type: image/jpeg

Notes

I have set formDataArrayFormat to 'indices' by default to preserve the current behavior. We might want to synchronize formDataArrayFormat and queryStringArrayFormat values at some point (or even introduce more general option), but this could be a breaking change.

@reinink reinink changed the base branch from master to v1 October 9, 2024 15:25
@skryukov skryukov force-pushed the form-data-array-serialization branch from ac60797 to 8bd4dec Compare October 9, 2024 17:45
@skryukov
Copy link
Contributor Author

Since 2.0 is coming, what if we deprecate queryStringArrayFormat and instead of formDataArrayFormat introduce just arrayFormat or arraySerializationFormat that controls both formData and query serialization?

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.

1 participant