mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-25 07:16:40 +00:00
Add support for query parameters of type array
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 } }
|
||||
|
||||
Reference in New Issue
Block a user