From e0ae445b68adeecbdb3c69a742647bdf12a8bd37 Mon Sep 17 00:00:00 2001 From: Justin Smestad Date: Fri, 5 Dec 2014 14:39:21 -0700 Subject: [PATCH 1/3] extra_params use Hash#deep_merge! instead of merge This allows support for this use case: ```ruby post '/post' do parameter :title, 'Title of post', scope: :post parameter :author_id, 'Integer of author', scope: :post let(:title) { 'My First Post' } let(:author_id) { '8' } example_request 'returns successful' do expect(response_body).to have_key(:id) end example_request 'fails with malformed id', post: {author_id: 'asdf' } do expect(response_body).to have_key(:errors) end end ``` Before this change the params changes from `post: {title: 'My First Post', author_id: 'asdf'}` to `post: {author_id: 'asdf'}`. `Hash#deep_merge!` resolves this. --- lib/rspec_api_documentation/dsl/endpoint.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rspec_api_documentation/dsl/endpoint.rb b/lib/rspec_api_documentation/dsl/endpoint.rb index 64f939ee..64eb6958 100644 --- a/lib/rspec_api_documentation/dsl/endpoint.rb +++ b/lib/rspec_api_documentation/dsl/endpoint.rb @@ -66,7 +66,7 @@ def params parameters = example.metadata.fetch(:parameters, {}).inject({}) do |hash, param| set_param(hash, param) end - parameters.merge!(extra_params) + parameters.deep_merge!(extra_params) parameters end From 608da0c174c1ee9541e7b83e1e2227d29c3c63fa Mon Sep 17 00:00:00 2001 From: Justin Smestad Date: Fri, 5 Dec 2014 14:53:55 -0700 Subject: [PATCH 2/3] require deep_merge explicitly --- lib/rspec_api_documentation.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/rspec_api_documentation.rb b/lib/rspec_api_documentation.rb index a8a5ce6f..7066409b 100644 --- a/lib/rspec_api_documentation.rb +++ b/lib/rspec_api_documentation.rb @@ -1,6 +1,7 @@ require 'active_support' require 'active_support/inflector' require 'active_support/core_ext/hash/conversions' +require 'active_support/core_ext/hash/deep_merge' require 'cgi' require 'json' From 09ccb97e36a476b8b668b6b2e861984829209217 Mon Sep 17 00:00:00 2001 From: Justin Smestad Date: Mon, 8 Dec 2014 09:59:29 -0700 Subject: [PATCH 3/3] Add spec to verify deep_merge functionality --- lib/rspec_api_documentation/dsl/endpoint.rb | 3 ++- spec/dsl_spec.rb | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/rspec_api_documentation/dsl/endpoint.rb b/lib/rspec_api_documentation/dsl/endpoint.rb index 64eb6958..698b62c0 100644 --- a/lib/rspec_api_documentation/dsl/endpoint.rb +++ b/lib/rspec_api_documentation/dsl/endpoint.rb @@ -38,7 +38,7 @@ def do_request(extra_params = {}) if method == :get && !query_string.blank? path_or_query += "?#{query_string}" else - if respond_to?(:raw_post) + if respond_to?(:raw_post) params_or_body = raw_post else formatter = RspecApiDocumentation.configuration.post_body_formatter @@ -128,6 +128,7 @@ def rspec_api_documentation_client def extra_params return {} if @extra_params.nil? @extra_params.inject({}) do |h, (k, v)| + v = v.is_a?(Hash) ? v.stringify_keys : v h[k.to_s] = v h end diff --git a/spec/dsl_spec.rb b/spec/dsl_spec.rb index a5509c86..81379081 100644 --- a/spec/dsl_spec.rb +++ b/spec/dsl_spec.rb @@ -362,6 +362,9 @@ context "auto request" do post "/orders" do parameter :order_type, "Type of order" + parameter :amount, "Amount of order", scope: :order + parameter :name, "Name of order", scope: :order + context "no extra params" do before do @@ -382,6 +385,17 @@ example_request "should take an optional parameter hash", :order_type => "big" end + + context "extra options for do_request with scoped hash" do + before do + expect(client).to receive(:post).with("/orders", {"order_type" => "big", "order" => {"amount" => "19.99", "name" => "Friday Order"}}, nil) + end + + let(:amount) { '19.99' } + let(:name) { 'Monday Order' } + + example_request "should deep merge the optional parameter hash", {:order_type => 'big', :order => {:name => 'Friday Order'}} + end end end