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

Consider AC::Parameters as Hash in url_for #42603

Merged
merged 1 commit into from
Jun 25, 2021

Conversation

casperisfine
Copy link
Contributor

@casperisfine casperisfine commented Jun 25, 2021

Ref: #42020

It's not uncommon to want to forward request parameters, as such the :params parameter of url_for often receive an AC::Parameters instance.

That previous PR did break our app quite extensively, and while I'm not 100% certain that behavior was intended, I can say with certainty that it's common.

@@ -822,7 +822,7 @@ def url_for(options, route_name = nil, url_strategy = UNKNOWN, method_name = nil
path = route_with_params.path(method_name)
params = route_with_params.params

if options[:params].is_a?(Hash)
if options[:params].is_a?(Hash) || options[:params].is_a?(ActionController::Parameters)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that anything mergeable should fall in here. Instead of having an exhaustive list of mergeable types 🤔

Suggested change
if options[:params].is_a?(Hash) || options[:params].is_a?(ActionController::Parameters)
if options[:params].respond_to? :to_h

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe yeah. Would need to be :to_hash though, as to_h is explicit casting, and to_hash implicit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, did just that. If that's good with you I'll merge on green.

Ref: rails#42020

It's not uncommon to want to forward request parameters,
as such the `:params` paramter of `url_for` often receive
an AC::Parameters instance.
params.merge! options[:params]
elsif options.key? :params
params[:params] = options[:params]
if options.key? :params
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why you made a nest conditional. You already use the safe operator for checking options[:params]&.respond_to?(:to_hash)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if options.key? :params
if options[:params]&.respond_to?(:to_hash)
params.merge! options[:params]
elsif options.key? :params
params[:params] = options[:params]
end

@byroot byroot merged commit 93b0b30 into rails:main Jun 25, 2021
@softbrada
Copy link

can this be backported to v6.x please?

@byroot
Copy link
Member

byroot commented Jan 5, 2023

No. This is a fix for a change that wasn't applied in 6.x. So not sure what you are after, but there's nothing to backport here.

Additionally 6.x is security fixes only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants