Skip to content

Commit 5f867e0

Browse files
committed
Does not evaluate original parameter name when custom method is provided
1 parent 3ad1ab7 commit 5f867e0

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

lib/rspec_api_documentation/dsl/endpoint/set_param.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def initialize(parent, hash, param)
1010

1111
def call
1212
return hash if path_params.include?(path_name)
13-
return hash unless method_name
13+
return hash unless param_method_name
1414

1515
hash.deep_merge build_param_hash(key_scope || [key])
1616
end
@@ -44,16 +44,18 @@ def path_params
4444
example.metadata[:route].scan(/:(\w+)/).flatten
4545
end
4646

47-
def method_name
48-
@method_name ||= begin
49-
[custom_method_name, scoped_key, key].find do |name|
50-
name && example_group.respond_to?(name)
51-
end
47+
def param_method_name
48+
if custom_method_name
49+
custom_method_name if example_group.respond_to?(custom_method_name)
50+
elsif scoped_key && example_group.respond_to?(scoped_key)
51+
scoped_key
52+
elsif key && example_group.respond_to?(key)
53+
key
5254
end
5355
end
5456

5557
def build_param_hash(keys)
56-
value = keys[1] ? build_param_hash(keys[1..-1]) : example_group.send(method_name)
58+
value = keys[1] ? build_param_hash(keys[1..-1]) : example_group.send(param_method_name)
5759
{ keys[0].to_s => value }
5860
end
5961
end

spec/dsl_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'spec_helper'
22
require 'rspec_api_documentation/dsl'
33
require 'net/http'
4+
require "rack/test"
45

56
describe "Non-api documentation specs" do
67
it "should not be polluted by the rspec api dsl" do |example|
@@ -396,6 +397,36 @@
396397
do_request
397398
end
398399
end
400+
401+
context "with reserved name parameter" do
402+
context "without custom method name" do
403+
parameter :status, "Filter order by status"
404+
405+
example "does not work as expected" do
406+
expect { do_request }.to raise_error Rack::Test::Error, /No response yet/
407+
end
408+
end
409+
410+
context "with custom method name" do
411+
parameter :status, "Filter order by status", method: :status_param
412+
413+
context "when parameter value is not specified" do
414+
example "does not serialize param" do
415+
expect(client).to receive(method).with("/orders", anything, anything)
416+
do_request
417+
end
418+
end
419+
420+
context "when parameter value is specified" do
421+
let(:status_param) { "pending" }
422+
423+
example "serializes param" do
424+
expect(client).to receive(method).with("/orders?status=pending", anything, anything)
425+
do_request
426+
end
427+
end
428+
end
429+
end
399430
end
400431
end
401432

0 commit comments

Comments
 (0)