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

FormData Handling Nested Arrays Incorrectly in Rails 7.1.2 #50227

Closed
makoto-atarashi opened this issue Dec 1, 2023 · 5 comments
Closed

FormData Handling Nested Arrays Incorrectly in Rails 7.1.2 #50227

makoto-atarashi opened this issue Dec 1, 2023 · 5 comments
Labels
actionpack more-information-needed When reporter needs to provide more information

Comments

@makoto-atarashi
Copy link

Issue Overview

After upgrading Rails from version 7.0.6 to 7.1.2, FormData that is including nested array sent from frontend is being received differently on backend, causing processing troubles.

Steps to Reproduce

  1. Use the following frontend JavaScript code, which was working correctly with Rails 7.0.6, to send data to backend.
let formData = new FormData();

formData.append("items[][foo]", "FOO");
formData.append("items[][bar]", "BAR");
formData.append("items[][baz]", "BAZ");
formData.append("items[][list][][a]", "A");
formData.append("items[][list][][b]", "B");

fetch("/api_url", {
  method: "POST",
  body: formData
})
  .then(response => response.json())
  .then(data => {
    console.log(data);
  })
  .catch(error => {
    console.error(error);
  });
  1. Upgrade to Rails 7.1.2 and use the same frontend code to send data to the backend again.

Expected Behavior

Expecting the data to be received on Rails 7.1.2 as it was on Rails 7.0.6.

Actual Behavior

Data received on Rails 7.0.6 was in the following format:

Parameters: {"items"=>[{"foo"=>"FOO", "bar"=>"BAR", "baz"=>"BAZ", "list"=>[{"a"=>"A", "b"=>"B"}]}], "host"=>"example.com"}

However, on Rails 7.1.2, it has changed as follows, and params[:items][:list] is not received as an array as expected:

Parameters: {"items"=>[{"foo"=>"FOO", "bar"=>"BAR", "baz"=>"BAZ", "list[]a"=>"A", "list[]b"=>"B"}], "host"=>"example.com"}

Environment

Rails Version: 7.0.6 -> 7.1.2
Ruby version: ruby 3.2.2-p53
Front End: JavaScript, Fetch API

Specific Issue Points

On Rails 7.0.6, params[:items][:list] was correctly received as an array, but on Rails 7.1.2, params[:items][:list] is not received as an array as expected.

@fatkodima
Copy link
Member

Same as (similar to) #49783 ?

@orhantoy
Copy link
Contributor

orhantoy commented Dec 1, 2023

I don't see a difference between 7.0.6 and 7.1.2 when using that exact JS frontend code with new vanilla Rails apps.
Do you have special middleware or callbacks in your controllers that could be doing something to the params?

@makoto-atarashi
Copy link
Author

Thank you for your comment. After conducting further investigation, it seems that the root cause of this issue lies in the upgrade of the Rack middleware rather than in Rails itself. Considering this, I am inclined to withdraw this issue here, as it appears to be more relevant to the Rack middleware.

Would you agree with closing this issue given the context provided?
Your thoughts or any additional insights on this matter would be appreciated.

@orhantoy
Copy link
Contributor

orhantoy commented Dec 4, 2023

@makoto-atarashi That rack issue does not seem to apply to the issue you're having and I couldn't reproduce the params you're seeing logged regardless of the version of rack. Would you mind reproducing the issue with a small Rails app and sharing it here publicly?

@skipkayhil skipkayhil added the more-information-needed When reporter needs to provide more information label Dec 12, 2023
@rails-bot
Copy link

rails-bot bot commented Dec 26, 2023

This issue has been automatically closed because there has been no follow-up response from the original author. We currently don't have enough information in order to take action. Please reach out if you have any additional information that will help us move this issue forward.

@rails-bot rails-bot bot closed this as completed Dec 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
actionpack more-information-needed When reporter needs to provide more information
Projects
None yet
Development

No branches or pull requests

4 participants