Skip to content

Commit 5f55530

Browse files
committed
Merge pull request #221 from kevintraver/nested-scope
Ability to set nested scopes
2 parents e9e1198 + 2e08148 commit 5f55530

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

lib/rspec_api_documentation/dsl/endpoint.rb

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,23 @@ def delete_extra_param(key)
148148

149149
def set_param(hash, param)
150150
key = param[:name]
151-
return hash if !respond_to?(key) || in_path?(key)
151+
return hash if in_path?(key)
152152

153-
if param[:scope]
154-
hash[param[:scope].to_s] ||= {}
155-
hash[param[:scope].to_s][key] = send(key)
156-
else
157-
hash[key] = send(key)
153+
keys = [param[:scope], key].flatten.compact
154+
method_name = keys.join('_')
155+
156+
unless respond_to?(method_name)
157+
method_name = key
158+
return hash unless respond_to?(method_name)
158159
end
159160

160-
hash
161+
hash.deep_merge(build_param_hash(keys, method_name))
161162
end
163+
164+
def build_param_hash(keys, method_name)
165+
value = keys[1] ? build_param_hash(keys[1..-1], method_name) : send(method_name)
166+
{ keys[0].to_s => value }
167+
end
168+
162169
end
163170
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)