Skip to content

Commit

Permalink
Transform the mime object to symbol when registering the parsers
Browse files Browse the repository at this point in the history
This will keep our current API working without having the users to
change their codebases.
  • Loading branch information
rafaelfranca committed Feb 23, 2016
1 parent 97ed810 commit a087cf4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
17 changes: 13 additions & 4 deletions actionpack/lib/action_dispatch/http/parameters.rb
@@ -1,6 +1,8 @@
module ActionDispatch
module Http
module Parameters
extend ActiveSupport::Concern

PARAMETERS_KEY = 'action_dispatch.request.path_parameters'

DEFAULT_PARSERS = {
Expand All @@ -10,13 +12,20 @@ module Parameters
}
}

def self.included(klass)
class << klass
attr_accessor :parameter_parsers
included do
class << self
attr_reader :parameter_parsers
end

klass.parameter_parsers = DEFAULT_PARSERS
self.parameter_parsers = DEFAULT_PARSERS
end

module ClassMethods
def parameter_parsers=(parsers) # :nodoc:
@parameter_parsers = parsers.transform_keys { |key| key.respond_to?(:symbol) ? key.symbol : key }
end
end

# Returns both GET and POST \parameters in a single hash.
def parameters
params = get_header("action_dispatch.request.parameters")
Expand Down
1 change: 1 addition & 0 deletions actionpack/lib/action_dispatch/middleware/params_parser.rb
Expand Up @@ -37,6 +37,7 @@ def original_exception
# The +parsers+ argument can take Hash of parsers where key is identifying
# content mime type, and value is a lambda that is going to process data.
def self.new(app, parsers = {})
parsers = parsers.transform_keys { |key| key.respond_to?(:symbol) ? key.symbol : key }
ActionDispatch::Request.parameter_parsers = ActionDispatch::Request::DEFAULT_PARSERS.merge(parsers)
app
end
Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/controller/webservice_test.rb
Expand Up @@ -65,7 +65,7 @@ def test_put_json

def test_register_and_use_json_simple
with_test_route_set do
with_params_parsers json: Proc.new { |data| ActiveSupport::JSON.decode(data)['request'].with_indifferent_access } do
with_params_parsers Mime[:json] => Proc.new { |data| ActiveSupport::JSON.decode(data)['request'].with_indifferent_access } do
post "/",
params: '{"request":{"summary":"content...","title":"JSON"}}',
headers: { 'CONTENT_TYPE' => 'application/json' }
Expand Down

0 comments on commit a087cf4

Please sign in to comment.