Skip to content

Commit

Permalink
Merge pull request #26017 from kaspth/parameters-yaml-format-backward…
Browse files Browse the repository at this point in the history
…scompatibility

Make Parameters support legacy YAML encodings.
  • Loading branch information
kaspth committed Aug 2, 2016
1 parent f1311a2 commit 77d0f12
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
19 changes: 19 additions & 0 deletions actionpack/lib/action_controller/metal/strong_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
require 'rack/test'
require 'stringio'
require 'set'
require 'yaml'

# Wire up YAML format compatibility with Rails 4.2. Makes the YAML parser call
# `init_with` when it encounters `!ruby/hash-with-ivars:ActionController::Parameters`,
# instead of trying to parse it as a regular hash subclass.
YAML.load_tags['!ruby/hash-with-ivars:ActionController::Parameters'] = 'ActionController::Parameters'

module ActionController
# Raised when a required parameter is missing.
Expand Down Expand Up @@ -604,6 +610,19 @@ def inspect
"<#{self.class} #{@parameters} permitted: #{@permitted}>"
end

def init_with(coder) # :nodoc:
if coder.map['elements']
# YAML's Hash subclass format from Rails 4.2, where keys and values
# were stored under an elements hash and `permitted` within an ivars hash.
@parameters = coder.map['elements'].with_indifferent_access
@permitted = coder.map['ivars'][:@permitted]
else
# YAML's Object format. Only needed because of the format
# backwardscompability above, otherwise equivalent to YAML's initialization.
@parameters, @permitted = coder.map['parameters'], coder.map['permitted']
end
end

def method_missing(method_sym, *args, &block)
if @parameters.respond_to?(method_sym)
message = <<-DEPRECATE.squish
Expand Down
32 changes: 32 additions & 0 deletions actionpack/test/controller/parameters/serialization_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'abstract_unit'
require 'action_controller/metal/strong_parameters'
require 'active_support/core_ext/string/strip'

class ParametersSerializationTest < ActiveSupport::TestCase
test 'yaml serialization' do
assert_equal <<-end_of_yaml.strip_heredoc, YAML.dump(ActionController::Parameters.new(key: :value))
--- !ruby/object:ActionController::Parameters
parameters: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
key: :value
permitted: false
end_of_yaml
end

test 'yaml deserialization' do
params = ActionController::Parameters.new(key: :value)
roundtripped = YAML.load(YAML.dump(params))

assert_equal params, roundtripped
assert_not roundtripped.permitted?
end

test 'yaml backwardscompatible with hash inheriting parameters' do
assert_equal ActionController::Parameters.new(key: :value), YAML.load(<<-end_of_yaml.strip_heredoc)
--- !ruby/hash-with-ivars:ActionController::Parameters
elements:
key: :value
ivars:
:@permitted: false
end_of_yaml
end
end

0 comments on commit 77d0f12

Please sign in to comment.