Skip to content

Commit 1c7f917

Browse files
committed
Merge pull request #224 from cover/master
Add the ability to set the value of nested params
2 parents de69a7a + ccad82f commit 1c7f917

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,8 @@ Special values:
392392
* `:required => true` Will display a red '*' to show it's required
393393
* `:scope => :the_scope` Will scope parameters in the hash, scoping can be nested. See example
394394

395+
The value of scoped parameters can be set with both scoped (`let(:order_item_item_id)`) and unscoped (`let(:item_id)`) methods. It always searches for the scoped method first and falls back to the unscoped method.
396+
395397
```ruby
396398
resource "Orders" do
397399
parameter :auth_token, "Authentication Token"
@@ -403,8 +405,8 @@ resource "Orders" do
403405
parameter :item, "Order items", :scope => :order
404406
parameter :item_id, "Item id", :scope => [:order, :item]
405407

406-
let(:name) { "My Order" }
407-
let(:item_id) { 1 }
408+
let(:name) { "My Order" } # OR let(:order_name) { "My Order" }
409+
let(:item_id) { 1 } # OR let(:order_item_item_id) { 1 }
408410

409411
example "Creating an order" do
410412
params.should eq({

lib/rspec_api_documentation/dsl/endpoint.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,12 @@ def delete_extra_param(key)
148148

149149
def set_param(hash, param)
150150
key = param[:name]
151-
return hash if in_path?(key)
152151

153152
keys = [param[:scope], key].flatten.compact
154153
method_name = keys.join('_')
155154

155+
return hash if in_path?(method_name)
156+
156157
unless respond_to?(method_name)
157158
method_name = key
158159
return hash unless respond_to?(method_name)

spec/dsl_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,12 @@
105105
put "/orders/:id" do
106106
parameter :type, "The type of drink you want.", :required => true
107107
parameter :size, "The size of drink you want.", :required => true
108+
parameter :id, 'The ID of the resource.', :required => true, scope: :data
108109
parameter :note, "Any additional notes about your order."
109110

110111
let(:type) { "coffee" }
111112
let(:size) { "medium" }
113+
let(:data_id) { 2 }
112114

113115
let(:id) { 1 }
114116

@@ -129,6 +131,10 @@
129131
end
130132
end
131133

134+
it 'should set the scoped data ID' do
135+
expect(params['data']).to eq({'id' => 2})
136+
end
137+
132138
it "should allow extra parameters to be passed in" do
133139
expect(client).to receive(method).with(path, params.merge("extra" => true), nil)
134140
do_request(:extra => true)

0 commit comments

Comments
 (0)