Merge pull request #112 from akurashev/master

Fix Authorization header missing and duplicating
This commit is contained in:
Richard Morris 2018-04-23 15:49:32 -07:00 committed by GitHub
commit 12daacf9a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 1 deletions

View File

@ -29,12 +29,15 @@ module Rswag
path_item_params = metadata[:path_item][:parameters] || [] path_item_params = metadata[:path_item][:parameters] || []
security_params = derive_security_params(metadata, swagger_doc) security_params = derive_security_params(metadata, swagger_doc)
operation_params operation_params = operation_params
.concat(path_item_params) .concat(path_item_params)
.concat(security_params) .concat(security_params)
.map { |p| p['$ref'] ? resolve_parameter(p['$ref'], swagger_doc) : p } .map { |p| p['$ref'] ? resolve_parameter(p['$ref'], swagger_doc) : p }
.uniq { |p| p[:name] } .uniq { |p| p[:name] }
.reject { |p| p[:required] == false && !example.respond_to?(p[:name]) } .reject { |p| p[:required] == false && !example.respond_to?(p[:name]) }
metadata[:operation][:parameters] = operation_params unless operation_params.empty?
operation_params
end end
def derive_security_params(metadata, swagger_doc) def derive_security_params(metadata, swagger_doc)

View File

@ -235,6 +235,34 @@ module Rswag
expect(request[:headers]).to eq('api_key' => 'foobar') expect(request[:headers]).to eq('api_key' => 'foobar')
end end
end end
context 'in header with no other params' do
let(:key_location) { :header }
it 'adds name and example value to the headers' do
expect(request[:headers]).to eq('api_key' => 'foobar')
expect(metadata[:operation][:parameters]).to(
include(name: 'api_key', in: :header, type: :string, required: true)
)
end
end
context 'in header with auth param already added' do
let(:key_location) { :header }
before do
metadata[:operation][:parameters] = [
{ name: 'q1', in: :query, type: :string },
{ name: 'api_key', in: :header, type: :string }
]
allow(example).to receive(:q1).and_return('foo')
allow(example).to receive(:api_key).and_return('foobar')
end
it 'adds authorization parameter only once' do
expect(request[:headers]).to eq('api_key' => 'foobar')
expect(metadata[:operation][:parameters].size).to eq 2
end
end
end end
context 'oauth2' do context 'oauth2' do