From eb10496a1c3d9fe58b91aa0ec17d9f218738d5dc Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 9 Jul 2015 14:56:26 -0700 Subject: drop runtime conditionals in parameter parsing If we only deal with proc objects, then we can eliminate type checking in the parameter parsing middleware --- .../action_dispatch/middleware/params_parser.rb | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) (limited to 'actionpack/lib/action_dispatch') diff --git a/actionpack/lib/action_dispatch/middleware/params_parser.rb b/actionpack/lib/action_dispatch/middleware/params_parser.rb index 29d43faeed..3776aa78ed 100644 --- a/actionpack/lib/action_dispatch/middleware/params_parser.rb +++ b/actionpack/lib/action_dispatch/middleware/params_parser.rb @@ -13,7 +13,13 @@ module ActionDispatch end end - DEFAULT_PARSERS = { Mime::JSON => :json } + DEFAULT_PARSERS = { + Mime::JSON => lambda { |raw_post| + data = ActiveSupport::JSON.decode(raw_post) + data = {:_json => data} unless data.is_a?(Hash) + Request::Utils.deep_munge(data).with_indifferent_access + } + } def initialize(app, parsers = {}) @app, @parsers = app, DEFAULT_PARSERS.merge(parsers) @@ -33,20 +39,10 @@ module ActionDispatch return false if request.content_length.zero? - strategy = @parsers[request.content_mime_type] + strategy = @parsers.fetch(request.content_mime_type) { return false } - return false unless strategy + strategy.call(request.raw_post) - case strategy - when Proc - strategy.call(request.raw_post) - when :json - data = ActiveSupport::JSON.decode(request.raw_post) - data = {:_json => data} unless data.is_a?(Hash) - Request::Utils.deep_munge(data).with_indifferent_access - else - false - end rescue => e # JSON or Ruby code block errors logger(env).debug "Error occurred while parsing request parameters.\nContents:\n\n#{request.raw_post}" -- cgit v1.2.3