Skip to content

Commit eb10496

Browse files
committed
drop runtime conditionals in parameter parsing
If we only deal with proc objects, then we can eliminate type checking in the parameter parsing middleware
1 parent d37f01b commit eb10496

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

actionpack/lib/action_dispatch/middleware/params_parser.rb

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ def initialize(message, original_exception)
1313
end
1414
end
1515

16-
DEFAULT_PARSERS = { Mime::JSON => :json }
16+
DEFAULT_PARSERS = {
17+
Mime::JSON => lambda { |raw_post|
18+
data = ActiveSupport::JSON.decode(raw_post)
19+
data = {:_json => data} unless data.is_a?(Hash)
20+
Request::Utils.deep_munge(data).with_indifferent_access
21+
}
22+
}
1723

1824
def initialize(app, parsers = {})
1925
@app, @parsers = app, DEFAULT_PARSERS.merge(parsers)
@@ -33,20 +39,10 @@ def parse_formatted_parameters(env)
3339

3440
return false if request.content_length.zero?
3541

36-
strategy = @parsers[request.content_mime_type]
42+
strategy = @parsers.fetch(request.content_mime_type) { return false }
3743

38-
return false unless strategy
44+
strategy.call(request.raw_post)
3945

40-
case strategy
41-
when Proc
42-
strategy.call(request.raw_post)
43-
when :json
44-
data = ActiveSupport::JSON.decode(request.raw_post)
45-
data = {:_json => data} unless data.is_a?(Hash)
46-
Request::Utils.deep_munge(data).with_indifferent_access
47-
else
48-
false
49-
end
5046
rescue => e # JSON or Ruby code block errors
5147
logger(env).debug "Error occurred while parsing request parameters.\nContents:\n\n#{request.raw_post}"
5248

0 commit comments

Comments
 (0)