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

drop runtime conditionals in parameter parsing

If we only deal with proc objects, then we can eliminate type checking
in the parameter parsing middleware
This commit is contained in:
Aaron Patterson 2015-07-09 14:56:26 -07:00
parent d37f01b931
commit eb10496a1c

View file

@ -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}"