From f8c882001cbe8942db7155deef6de8e2eedd4d9b Mon Sep 17 00:00:00 2001 From: richie Date: Wed, 12 Oct 2016 14:10:01 -0700 Subject: [PATCH] Add support for query parameters of type array --- ci/deploy.sh | 22 ++++----- .../lib/rswag/specs/request_factory.rb | 26 ++++++++-- .../spec/rswag/specs/request_factory_spec.rb | 47 +++++++++++++++++++ 3 files changed, 80 insertions(+), 15 deletions(-) diff --git a/ci/deploy.sh b/ci/deploy.sh index 813b713..26b4cc6 100755 --- a/ci/deploy.sh +++ b/ci/deploy.sh @@ -34,17 +34,17 @@ echo '' echo 'Type the version no, followed by [ENTER]:' read version -#echo '##### rswag-api #####' -#cd $ROOT_PATH/rswag-api -#gem push rswag-api-$version.gem -# -#echo '##### rswag-specs #####' -#cd $ROOT_PATH/rswag-specs -#gem push rswag-specs-$version.gem -# -#echo '##### rswag-ui #####' -#cd $ROOT_PATH/rswag-ui -#gem push rswag-ui-$version.gem +echo '##### rswag-api #####' +cd $ROOT_PATH/rswag-api +gem push rswag-api-$version.gem + +echo '##### rswag-specs #####' +cd $ROOT_PATH/rswag-specs +gem push rswag-specs-$version.gem + +echo '##### rswag-ui #####' +cd $ROOT_PATH/rswag-ui +gem push rswag-ui-$version.gem echo '##### rswag #####' cd $ROOT_PATH/rswag diff --git a/rswag-specs/lib/rswag/specs/request_factory.rb b/rswag-specs/lib/rswag/specs/request_factory.rb index 50dcc2a..39ade56 100644 --- a/rswag-specs/lib/rswag/specs/request_factory.rb +++ b/rswag-specs/lib/rswag/specs/request_factory.rb @@ -12,15 +12,15 @@ module Rswag def build_fullpath(example) @api_metadata[:path].dup.tap do |t| - parameters_in(:path).each { |p| t.gsub!("{#{p[:name]}}", example.send(p[:name]).to_s) } - t.concat(build_query(example)) t.prepend(@global_metadata[:basePath] || '') + parameters_in(:path).each { |p| t.gsub!("{#{p[:name]}}", example.send(p[:name]).to_s) } + t.concat(build_query_string(example)) end end - def build_query(example) + def build_query_string(example) query_string = parameters_in(:query) - .map { |p| "#{p[:name]}=#{example.send(p[:name])}" } + .map { |p| build_query_string_part(p, example.send(p[:name])) } .join('&') query_string.empty? ? '' : "?#{query_string}" @@ -65,6 +65,24 @@ module Rswag definitions.values.select { |d| d[:type] == :apiKey } end end + + def build_query_string_part(param, value) + return "#{param[:name]}=#{value.to_s}" unless param[:type].to_sym == :array + + name = param[:name] + case param[:collectionFormat] + when :ssv + "#{name}=#{value.join(' ')}" + when :tsv + "#{name}=#{value.join('\t')}" + when :pipes + "#{name}=#{value.join('|')}" + when :multi + value.map { |v| "#{name}=#{v}" }.join('&') + else + "#{name}=#{value.join(',')}" # csv is default + end + end end end end diff --git a/rswag-specs/spec/rswag/specs/request_factory_spec.rb b/rswag-specs/spec/rswag/specs/request_factory_spec.rb index 61a9f1f..32106f3 100644 --- a/rswag-specs/spec/rswag/specs/request_factory_spec.rb +++ b/rswag-specs/spec/rswag/specs/request_factory_spec.rb @@ -49,6 +49,53 @@ module Rswag end end + context "'query' parameter of type 'array'" do + before do + api_metadata[:operation][:parameters] << { + name: 'things', + in: :query, + type: :array, + collectionFormat: collectionFormat + } + allow(example).to receive(:things).and_return([ 'foo', 'bar' ]) + end + + context 'collectionFormat = csv' do + let(:collectionFormat) { :csv } + it "formats as comma separated values" do + expect(path).to eq('/blogs/1/comments/2?things=foo,bar') + end + end + + context 'collectionFormat = ssv' do + let(:collectionFormat) { :ssv } + it "formats as space separated values" do + expect(path).to eq('/blogs/1/comments/2?things=foo bar') + end + end + + context 'collectionFormat = tsv' do + let(:collectionFormat) { :tsv } + it "formats as tab separated values" do + expect(path).to eq('/blogs/1/comments/2?things=foo\tbar') + end + end + + context 'collectionFormat = pipes' do + let(:collectionFormat) { :pipes } + it "formats as pipe separated values" do + expect(path).to eq('/blogs/1/comments/2?things=foo|bar') + end + end + + context 'collectionFormat = multi' do + let(:collectionFormat) { :multi } + it "formats as multiple parameter instances" do + expect(path).to eq('/blogs/1/comments/2?things=foo&things=bar') + end + end + end + context "global definition for 'api_key in query'" do before do global_metadata[:securityDefinitions] = { api_key: { type: :apiKey, name: 'api_key', in: :query } }