Skip to content

Commit b115d2b

Browse files
committed
Headers added before swaggerUi.load() call.
Add missing authorizations: headers Fix Rubocop offenses
1 parent ecede5f commit b115d2b

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* [#31](https://github.com/ruby-grape/grape-swagger-rails/pull/31): Support Swagger-UI docExpansion option - [@maruware](https://github.com/maruware).
88
* [#32](https://github.com/ruby-grape/grape-swagger-rails/pull/32): Fix Ruby 1.9.3 compatibility - [@suan](https://github.com/suan).
99
* [#39](https://github.com/ruby-grape/grape-swagger-rails/pull/39): Support CSS media queries - [@alexagranov](https://github.com/alexagranov).
10+
* [#42](https://github.com/ruby-grape/grape-swagger-rails/pull/42): Headers added on swaggerUi initialization (before swaggerUi.load() call) - [@sedx](https://github.com/sedx).
1011
* Your contribution here.
1112

1213
### 0.1.0 (February 5, 2015)

Diff for: app/views/grape_swagger_rails/application/index.html.erb

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@
1010
$(function () {
1111
var options = $("html").data('swagger-options');
1212

13+
var headers = {};
14+
<% GrapeSwaggerRails.options.headers.each_with_index do |(key, value), index| %>
15+
<%=raw "headers.header_#{index} = new SwaggerClient.ApiKeyAuthorization('#{CGI.escapeHTML(key)}', '#{CGI.escapeHTML(value)}', 'header');" %>
16+
<% end %>
17+
1318
window.swaggerUi = new SwaggerUi({
1419
url: options.app_url + options.url,
1520
dom_id: "swagger-ui-container",
1621
supportHeaderParams: true,
1722
supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'],
23+
authorizations: headers,
1824
onComplete: function(swaggerApi, swaggerUi){
1925
if('console' in window) {
2026
console.log("Loaded SwaggerUI")
@@ -50,9 +56,6 @@
5056

5157
window.swaggerUi.load();
5258

53-
<% GrapeSwaggerRails.options.headers.each_with_index do |(key, value), index| %>
54-
<%=raw "window.swaggerUi.api.clientAuthorizations.add('header_#{index}', new SwaggerClient.ApiKeyAuthorization('#{CGI.escapeHTML(key)}', '#{CGI.escapeHTML(value)}', 'header'));" %>
55-
<% end %>
5659
});
5760
</script>
5861
</head>

Diff for: spec/features/swagger_spec.rb

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
visit '/swagger'
2424
end
2525
it 'adds headers' do
26+
headers = page.evaluate_script('swaggerUi.api.clientAuthorizations')['authz']
27+
expect(headers.select { |key| key.to_s.match(/^header/) }).not_to be_blank
28+
expect(headers.fetch('header_0', {}).fetch('name', {})).to eq GrapeSwaggerRails.options.headers.keys.first
2629
find('#endpointListTogger_headers', visible: true).click
2730
first('a[href="#!/headers/GET_api_headers_format"]', visible: true).click
2831
click_button 'Try it out!'

0 commit comments

Comments
 (0)