Merge pull request #76 from ahobson/bugfix-optional-query-params

Support optional query parameters
This commit is contained in:
domaindrivendev 2017-07-13 16:35:48 -07:00 committed by GitHub
commit 8b937e3585
2 changed files with 18 additions and 0 deletions

View File

@ -20,6 +20,8 @@ module Rswag
def build_query_string(example)
query_string = parameters_in(:query)
.select { |p| p.fetch(:required, true) ||
example.respond_to?(p[:name]) }
.map { |p| build_query_string_part(p, example.send(p[:name])) }
.join('&')

View File

@ -48,6 +48,22 @@ module Rswag
end
end
context "optional 'query' parameters" do
before do
api_metadata[:operation][:parameters] << { name: 'q1', in: :query, type: 'string' }
api_metadata[:operation][:parameters] << { name: 'q2', in: :query, type: 'string', required: true }
api_metadata[:operation][:parameters] << { name: 'q3', in: :query, type: 'string', required: false }
api_metadata[:operation][:parameters] << { name: 'q4', in: :query, type: 'string', required: false }
allow(example).to receive(:q1).and_return('foo')
allow(example).to receive(:q2).and_return('bar')
allow(example).to receive(:q3).and_return('baz')
end
it "appends a query string using metadata and example values" do
expect(path).to eq('/blogs/1/comments/2?q1=foo&q2=bar&q3=baz')
end
end
context "'query' parameter of type 'array'" do
before do
api_metadata[:operation][:parameters] << {