Skip to content

Commit d6ed844

Browse files
Tyr0dblock
authored andcommitted
Support multiple headers at the same time
Swagger supports multiple headers but requires that each one uses a unique name. The existing approach overwrote the previous header definition. This also caused issues when changing the value of `#input_apiKey` as it stores the api key with the same name as all defined headers, leading to complete header loss on api key addition. This fix ensures each header is given a unique name. For more information see: https://github.com/swagger-api/swagger-ui#header-parameters
1 parent 3ed8b59 commit d6ed844

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### Next
22

3+
* [#6](https://github.com/TinkerDev/grape-swagger-rails/pull/6): Fix: support multiple predefined headers - [@Tyr0](https://github.com/tyr0).
34
* Your contribution here.
45

56
### 0.1.0 (February 5, 2015)

app/views/grape_swagger_rails/application/index.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
}
4747
});
4848

49-
<% GrapeSwaggerRails.options.headers.each_pair do |key, value| %>
50-
<%=raw "window.authorizations.add('key', new ApiKeyAuthorization('#{CGI.escapeHTML(key)}', '#{CGI.escapeHTML(value)}', 'header'));" %>
49+
<% GrapeSwaggerRails.options.headers.each_with_index do |(key, value), index| %>
50+
<%=raw "window.authorizations.add('header_#{index}', new ApiKeyAuthorization('#{CGI.escapeHTML(key)}', '#{CGI.escapeHTML(value)}', 'header'));" %>
5151
<% end %>
5252

5353
window.swaggerUi.load();

spec/features/swagger_spec.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,24 @@
1919
context "#headers" do
2020
before do
2121
GrapeSwaggerRails.options.headers['X-Test-Header'] = 'Test Value'
22+
GrapeSwaggerRails.options.headers['X-Another-Header'] = 'Another Value'
2223
visit '/swagger'
2324
end
2425
it 'adds headers' do
2526
find('#endpointListTogger_headers', visible: true).click
26-
find('a[href="#!/headers/GET_api_headers_format"]', visible: true).click
27+
first('a[href="#!/headers/GET_api_headers_format"]', visible: true).click
28+
click_button 'Try it out!'
29+
expect(page).to have_css "span.attribute", text: 'X-Test-Header'
30+
expect(page).to have_css "span.string", text: 'Test Value'
31+
end
32+
it 'supports multiple headers' do
33+
find('#endpointListTogger_headers', visible: true).click
34+
first('a[href="#!/headers/GET_api_headers_format"]', visible: true).click
2735
click_button 'Try it out!'
2836
expect(page).to have_css "span.attribute", text: 'X-Test-Header'
2937
expect(page).to have_css "span.string", text: 'Test Value'
38+
expect(page).to have_css "span.attribute", text: 'X-Another-Header'
39+
expect(page).to have_css "span.string", text: 'Another Value'
3040
end
3141
end
3242
context "#api_auth:basic" do

0 commit comments

Comments
 (0)