diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 94c0a6bf95c16ae95db958ddf1e808065e84d9ed..96eb8ac77dc5c97241ed53b6314dd57c0734ffe6 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Underscore dasherized keys in formatted requests [Jamis Buck] + * Add MimeResponds::Responder#any for managing multiple types with identical responses [Jamis Buck] * Make the xml_http_request testing method set the HTTP_ACCEPT header [Jamis Buck] diff --git a/actionpack/lib/action_controller/cgi_ext/cgi_methods.rb b/actionpack/lib/action_controller/cgi_ext/cgi_methods.rb index f092a41d47e70103cc997b72f0f091c63a62076b..cd70836c6edaf64d308063bcdfab3751ac420207 100755 --- a/actionpack/lib/action_controller/cgi_ext/cgi_methods.rb +++ b/actionpack/lib/action_controller/cgi_ext/cgi_methods.rb @@ -71,7 +71,7 @@ def self.parse_formatted_request_parameters(mime_type, raw_post_data) { node.node_name => node } end - params || {} + dasherize_keys(params || {}) rescue Object => e { "exception" => "#{e.message} (#{e.class})", "backtrace" => e.backtrace, "raw_post_data" => raw_post_data, "format" => mime_type } @@ -79,6 +79,18 @@ def self.parse_formatted_request_parameters(mime_type, raw_post_data) private + def self.dasherize_keys(params) + case params.class.to_s + when "Hash" + params.inject({}) do |h,(k,v)| + h[k.tr("-", "_")] = dasherize_keys(v) + h + end + else + params + end + end + # Splits the given key into several pieces. Example keys are 'name', 'person[name]', # 'person[name][first]', and 'people[]'. In each instance, an Array instance is returned. # 'person[name][first]' produces ['person', 'name', 'first']; 'people[]' produces ['people', ''] diff --git a/actionpack/lib/action_controller/mime_responds.rb b/actionpack/lib/action_controller/mime_responds.rb index 04ec1e5dd84dd6f73232a1f1582c17d51be8d784..140209a225e3e19f599472653100b4d330c1ce05 100644 --- a/actionpack/lib/action_controller/mime_responds.rb +++ b/actionpack/lib/action_controller/mime_responds.rb @@ -36,7 +36,7 @@ def custom(mime_type, *args, &block) @responses[mime_type] = block else if argument = args.first - eval("__mime_responder_arg__ = " + (argument.is_a?(String) ? "'" + argument + "'" : argument), @block_binding) + eval("__mime_responder_arg__ = #{argument.is_a?(String) ? argument.inspect : argument}", @block_binding) @responses[mime_type] = eval(DEFAULT_BLOCKS[(mime_type.to_sym.to_s + "_arg").to_sym], @block_binding) else @responses[mime_type] = eval(DEFAULT_BLOCKS[mime_type.to_sym], @block_binding) diff --git a/actionpack/test/controller/webservice_test.rb b/actionpack/test/controller/webservice_test.rb index c99abeaa9e5031687e2aece63cc7aa93df073254..90691e9e9cddc65f3eec598510809c46d411e501 100644 --- a/actionpack/test/controller/webservice_test.rb +++ b/actionpack/test/controller/webservice_test.rb @@ -19,7 +19,20 @@ class TestController < ActionController::Base session :off def assign_parameters - render :text => (@params.keys - ['controller', 'action']).sort.join(", ") + if params[:full] + render :text => dump_params_keys + else + render :text => (params.keys - ['controller', 'action']).sort.join(", ") + end + end + + def dump_params_keys(hash=params) + hash.keys.sort.inject("") do |s, k| + value = hash[k] + value = Hash === value ? "(#{dump_params_keys(value)})" : "" + s << ", " unless s.empty? + s << "#{k}#{value}" + end end def rescue_action(e) raise end @@ -88,16 +101,28 @@ def test_deprecated_request_methods assert_equal true, @controller.request.yaml_post? assert_equal false, @controller.request.xml_post? end + + def test_dasherized_keys_as_xml + ActionController::Base.param_parsers[Mime::XML] = :xml_simple + process('POST', 'application/xml', "\n...\n", true) + assert_equal 'action, controller, first_key(sub_key), full', @controller.response.body + end + + def test_dasherized_keys_as_yaml + ActionController::Base.param_parsers[Mime::YAML] = :yaml + process('POST', 'application/x-yaml', "---\nfirst-key:\n sub-key: ...\n", true) + assert_equal 'action, controller, first_key(sub_key), full', @controller.response.body + end private - def process(verb, content_type = 'application/x-www-form-urlencoded', data = '') + def process(verb, content_type = 'application/x-www-form-urlencoded', data = '', full=false) cgi = MockCGI.new({ 'REQUEST_METHOD' => verb, 'CONTENT_TYPE' => content_type, - 'QUERY_STRING' => "action=assign_parameters&controller=webservicetest/test", + 'QUERY_STRING' => "action=assign_parameters&controller=webservicetest/test#{"&full=1" if full}", "REQUEST_URI" => "/", "HTTP_HOST" => 'testdomain.com', "CONTENT_LENGTH" => data.size,