1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Implement ActionController::Parameters#to_query and #to_param

Previously it was raising an error because it may be unsafe to use those
methods in a unpermitted parameter. Now we delegate to to_h that already
raise an error when the Parameters instance is not permitted.

This also fix a bug when using `#to_query` in a hash that contains a
`ActionController::Parameters` instance and was returning the name of the
class in the string.
This commit is contained in:
Rafael Mendonça França 2017-04-11 22:21:14 -04:00
parent af878151db
commit 29333ddb69
No known key found for this signature in database
GPG key ID: FC23B6D0F1EEE948
2 changed files with 49 additions and 5 deletions

View file

@ -275,6 +275,34 @@ module ActionController
to_h.to_hash
end
# Returns a string representation of the receiver suitable for use as a URL
# query string:
#
# params = ActionController::Parameters.new({
# name: 'David',
# nationality: 'Danish'
# })
# params.to_query
# # => "name=David&nationality=Danish"
#
# An optional namespace can be passed to enclose key names:
#
# params = ActionController::Parameters.new({
# name: 'David',
# nationality: 'Danish'
# })
# params.to_query('user')
# # => "user%5Bname%5D=David&user%5Bnationality%5D=Danish"
#
# The string pairs "key=value" that conform the query string
# are sorted lexicographically in ascending order.
#
# This method is also aliased as +to_param+.
def to_query(*args)
to_h.to_query(*args)
end
alias_method :to_param, :to_query
# Returns an unsafe, unfiltered
# <tt>ActiveSupport::HashWithIndifferentAccess</tt> representation of this
# parameter.
@ -744,8 +772,6 @@ module ActionController
end
end
undef_method :to_param
# Returns duplicate of object including all parameters.
def deep_dup
self.class.new(@parameters.deep_dup).tap do |duplicate|

View file

@ -72,9 +72,27 @@ class ParametersRequireTest < ActiveSupport::TestCase
assert params.value?("cinco")
end
test "to_query is not supported" do
assert_raises(NoMethodError) do
ActionController::Parameters.new(foo: "bar").to_param
test "to_param works like in a Hash" do
params = ActionController::Parameters.new(nested: { key: "value" }).permit!
assert_equal({ nested: { key: "value" } }.to_param, params.to_param)
params = { root: ActionController::Parameters.new(nested: { key: "value" }).permit! }
assert_equal({ root: { nested: { key: "value" } } }.to_param, params.to_param)
assert_raise(ActionController::UnfilteredParameters) do
ActionController::Parameters.new(nested: { key: "value" }).to_param
end
end
test "to_query works like in a Hash" do
params = ActionController::Parameters.new(nested: { key: "value" }).permit!
assert_equal({ nested: { key: "value" } }.to_query, params.to_query)
params = { root: ActionController::Parameters.new(nested: { key: "value" }).permit! }
assert_equal({ root: { nested: { key: "value" } } }.to_query, params.to_query)
assert_raise(ActionController::UnfilteredParameters) do
ActionController::Parameters.new(nested: { key: "value" }).to_query
end
end
end