Skip to content

Commit 090c13a

Browse files
committed
Ability to set nested scopes
1 parent 0726538 commit 090c13a

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

lib/rspec_api_documentation/dsl/endpoint.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,13 @@ def set_param(hash, param)
150150
key = param[:name]
151151
return hash if !respond_to?(key) || in_path?(key)
152152

153-
if param[:scope]
154-
hash[param[:scope].to_s] ||= {}
155-
hash[param[:scope].to_s][key] = send(key)
153+
if scope = param[:scope]
154+
if scope.is_a?(Array)
155+
hash.merge!(scope.reverse.inject({key => send(key)}) { |a,n| { n.to_s => a }})
156+
else
157+
hash[scope.to_s] ||= {}
158+
hash[scope.to_s][key] = send(key)
159+
end
156160
else
157161
hash[key] = send(key)
158162
end

spec/dsl_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@
364364
parameter :order_type, "Type of order"
365365
parameter :amount, "Amount of order", scope: :order
366366
parameter :name, "Name of order", scope: :order
367+
parameter :street, "order location country", scope: [:order,:location,:address]
367368

368369

369370
context "no extra params" do
@@ -396,6 +397,16 @@
396397

397398
example_request "should deep merge the optional parameter hash", {:order_type => 'big', :order => {:name => 'Friday Order'}}
398399
end
400+
401+
context "extra options for do_request with nested scope" do
402+
before do
403+
expect(client).to receive(:post).with("/orders", {"order" => {"location" => {"address" => {"street" => "123 Main St"}}}}, nil)
404+
end
405+
406+
let(:street) { '123 Main St' }
407+
408+
example_request "should deep merge the optional parameter hash with nested scope"
409+
end
399410
end
400411
end
401412

0 commit comments

Comments
 (0)