Skip to content

Commit 97ed810

Browse files
meinacrafaelfranca
authored andcommitted
Use symbol of mime type instead of object to get correct parser
After registering new `:json` mime type `parsers.fetch` can't find the mime type because new mime type is not equal to old one. Using symbol of the mime type as key on parsers hash solves the problem. Closes rails#23766
1 parent 3537826 commit 97ed810

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

actionpack/lib/action_controller/test_case.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def initialize(env, session)
5252
self.session = session
5353
self.session_options = TestSession::DEFAULT_OPTIONS
5454
@custom_param_parsers = {
55-
Mime[:xml] => lambda { |raw_post| Hash.from_xml(raw_post)['hash'] }
55+
xml: lambda { |raw_post| Hash.from_xml(raw_post)['hash'] }
5656
}
5757
end
5858

@@ -105,7 +105,7 @@ def assign_parameters(routes, controller_path, action, parameters, generated_pat
105105
when :url_encoded_form
106106
data = non_path_parameters.to_query
107107
else
108-
@custom_param_parsers[content_mime_type] = ->(_) { non_path_parameters }
108+
@custom_param_parsers[content_mime_type.symbol] = ->(_) { non_path_parameters }
109109
data = non_path_parameters.to_query
110110
end
111111
end

actionpack/lib/action_dispatch/http/parameters.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Parameters
44
PARAMETERS_KEY = 'action_dispatch.request.path_parameters'
55

66
DEFAULT_PARSERS = {
7-
Mime[:json] => lambda { |raw_post|
7+
Mime[:json].symbol => -> (raw_post) {
88
data = ActiveSupport::JSON.decode(raw_post)
99
data.is_a?(Hash) ? data : {:_json => data}
1010
}
@@ -51,7 +51,7 @@ def path_parameters
5151
def parse_formatted_parameters(parsers)
5252
return yield if content_length.zero?
5353

54-
strategy = parsers.fetch(content_mime_type) { return yield }
54+
strategy = parsers.fetch(content_mime_type.symbol) { return yield }
5555

5656
begin
5757
strategy.call(raw_post)

actionpack/test/controller/webservice_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def test_put_json
6565

6666
def test_register_and_use_json_simple
6767
with_test_route_set do
68-
with_params_parsers Mime[:json] => Proc.new { |data| ActiveSupport::JSON.decode(data)['request'].with_indifferent_access } do
68+
with_params_parsers json: Proc.new { |data| ActiveSupport::JSON.decode(data)['request'].with_indifferent_access } do
6969
post "/",
7070
params: '{"request":{"summary":"content...","title":"JSON"}}',
7171
headers: { 'CONTENT_TYPE' => 'application/json' }
@@ -99,7 +99,7 @@ def test_dasherized_keys_as_json
9999
def test_parsing_json_doesnot_rescue_exception
100100
req = Class.new(ActionDispatch::Request) do
101101
def params_parsers
102-
{ Mime[:json] => Proc.new { |data| raise Interrupt } }
102+
{ json: Proc.new { |data| raise Interrupt } }
103103
end
104104

105105
def content_length; get_header('rack.input').length; end

actionpack/test/dispatch/request/json_params_parsing_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,22 @@ def teardown
150150
)
151151
end
152152

153+
test "parses json params after custom json mime type registered" do
154+
Mime::Type.register "application/json", :json, %w(application/vnd.api+json)
155+
assert_parses(
156+
{"user" => {"username" => "meinac"}, "username" => "meinac"},
157+
"{\"username\": \"meinac\"}", { 'CONTENT_TYPE' => 'application/json' }
158+
)
159+
end
160+
161+
test "parses json params after custom json mime type registered with synonym" do
162+
Mime::Type.register "application/json", :json, %w(application/vnd.api+json)
163+
assert_parses(
164+
{"user" => {"username" => "meinac"}, "username" => "meinac"},
165+
"{\"username\": \"meinac\"}", { 'CONTENT_TYPE' => 'application/vnd.api+json' }
166+
)
167+
end
168+
153169
private
154170
def assert_parses(expected, actual, headers = {})
155171
with_test_routing(UsersController) do

0 commit comments

Comments
 (0)