-
Notifications
You must be signed in to change notification settings - Fork 22k
fix #23640 allow strong parameters to accept nested arrays #23650
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
Conversation
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @arthurnn (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
0d1dbba
to
f1e93d8
Compare
r? @arthurnn |
r? |
+1 |
This issue has been open for almost a year now. Still facing this problem and having to write my own code to work around it. @rafaelfranca @fxn |
This is indeed a common use case of strong parameters. I am ok into us iterating in the current strong parameters we have, to add things like this. and optional types to parameters, for easy casting in the controller. |
I think this is just fine. I have more objections on other parts of the type casting spectrum. But let's evaluate on a case by case basis! |
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.
We will need a nice changelog entry for this change.
Thanks for taking the time .
allowed_elements = object.flatten.grep(Parameters).map { |el| yield el }.compact | ||
|
||
if object.first.is_a?(Array) | ||
permit_nested_array(allowed_elements.first, object) |
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.
why do we need the same is_a? Array
condition here, and in the recursive function? Could we just call it here, and have that condition there?
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.
I'm not sure if I understand your point,
but if we always calls permit_nested_array
even object is not a nested array, it will need to permit again the parameter, for example:
if we change the code
allowed_elements = object.flatten.grep(Parameters).map { |el| yield el }.compact
if object.first.is_a?(Array)
permit_nested_array(allowed_elements.first, object)
else
allowed_elements
end
for something like
allowed_elements = object.flatten.grep(Parameters).map { |el| yield el }.compact
allowed_elements.contact(allowed_elements, object)
when it's not a nested array, the method will try to permit again the elements,
please let me know your opinion, or if it was a missunderstood it,
Thanks
when Array | ||
permitted << permit_nested_array(elements, nested_object) | ||
else | ||
permitted << nested_object.select {|nested| elements.keys.include?(nested)} |
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.
It will rise error if nested_object is scalar type.
There are nested arrays with scalar types in GeoJSON
@rutaka-n good catch, I fixed the problem and added a test for it. |
r? @arthurnn |
Can we get some eyes on this? It's a painful bug to work around. The fix with tests and changelog are already submitted. We just need someone from the rails team to deal with the changelog merge conflict. |
@rafaelfranca Any objections? |
Does this change allow previously-rejected values to pass an existing permit pattern, or does it only add behaviour for a previously-invalid pattern form? |
@matthewd It just fixes a problem with the nested array pattern, like: |
@leonardoprg sorry, I missed your previous reply. I'm not sure how to best clarify my question, but it was an either-or: exactly one of those things is true, because they're opposites. Does this change allow previously-rejected values to pass an existing permit pattern? Is there an argument to The opposite would be that every argument A, that after this change permits value Y, previously did not permit any value X. |
sections: [[{ | ||
title: "Romance", | ||
description: "A Romance section" | ||
}]], |
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.
If this array had two sections I'd expect both sections to be permitted but only the first one is.
Also, I think this syntax is ambiguous.
Given params.permit(sections: [[[:title]]])
, what would be result for the following hashes ?
sections: [
[
{
title: "romance",
description: "a romance section"
}
],
[
{
title: "drama",
description: "a drama section"
}
]
],
And
sections: [
[
{
title: "romance",
description: "a romance section"
},
{
title: "drama",
description: "a drama section"
}
]
],
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.
I would permit both
I've added a PR for |
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
This pull request is related to issue
#23640
You guys can test it using this script:
https://gist.github.com/leonardoprg/ae6b3a48b880d5630c52