From dd103c2b40aa92596a355d0c44fe1ce45144d905 Mon Sep 17 00:00:00 2001 From: mur-wtag Date: Wed, 28 Sep 2016 12:55:36 +0600 Subject: [PATCH 1/6] - Authorization header issue resolved CORE-826 --- .../application/index.html.erb | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/app/views/grape_swagger_rails/application/index.html.erb b/app/views/grape_swagger_rails/application/index.html.erb index 676471d..54b789d 100644 --- a/app/views/grape_swagger_rails/application/index.html.erb +++ b/app/views/grape_swagger_rails/application/index.html.erb @@ -15,6 +15,15 @@ <%=raw "headers.header_#{index} = new SwaggerClient.ApiKeyAuthorization('#{CGI.escapeHTML(key)}', '#{CGI.escapeHTML(value)}', 'header');" %> <% end %> + // In very next line we are going to initialize `SwaggerUi`. But To initialize `SwaggerUi` we need to have authorization header. + // And the inside `onComplete` we are adding `Authorization Header` by calling `addApiKeyAuthorization()`. + // So, initialization of `SwaggerUi` fails and triggers `onFaiulre` :p + + // And this resolution is to add header before initialize `SwaggerUI` + // Some discussions: https://groups.google.com/forum/#!topic/swagger-swaggersocket/tJ0YHdTnBRk + // https://github.com/swagger-api/swagger-ui/issues/1171#issuecomment-100374999 + <%= raw "headers.header_#{GrapeSwaggerRails.options.headers.count} = new SwaggerClient.ApiKeyAuthorization(options.api_key_name, prepareApiKeyAuthorization(), options.api_key_type);" unless GrapeSwaggerRails.options.headers.keys.include?('Authorization') %> + window.swaggerUi = new SwaggerUi({ url: options.app_url + options.url, dom_id: "swagger-ui-container", @@ -42,6 +51,12 @@ }); function addApiKeyAuthorization() { + var api_key = $('#input_apiKey')[0].value; + if (api_key && api_key.trim() != "") + window.swaggerUi.api.clientAuthorizations.add("key", new SwaggerClient.ApiKeyAuthorization(options.api_key_name, prepareApiKeyAuthorization(), options.api_key_type)); + } + + function prepareApiKeyAuthorization() { var key = $('#input_apiKey')[0].value; if (key && key.trim() != "") { @@ -50,9 +65,9 @@ } else if (options.api_auth == 'bearer') { key = "Bearer " + key } - window.swaggerUi.api.clientAuthorizations.add("key", new SwaggerClient.ApiKeyAuthorization(options.api_key_name, key, options.api_key_type)); } - } + return key; + } $('#input_apiKey').change(addApiKeyAuthorization); From 2cad954480e6a5deff65af2ac950f3736479c6b3 Mon Sep 17 00:00:00 2001 From: mur-wtag Date: Mon, 3 Oct 2016 17:04:03 +0600 Subject: [PATCH 2/6] - spec will be failed CORE-826 --- .../grape_swagger_rails/application/index.html.erb | 2 +- spec/dummy/app/api/api.rb | 1 + spec/dummy/app/api/v1/resources/animals.rb | 11 +++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 spec/dummy/app/api/v1/resources/animals.rb diff --git a/app/views/grape_swagger_rails/application/index.html.erb b/app/views/grape_swagger_rails/application/index.html.erb index 54b789d..8e068ee 100644 --- a/app/views/grape_swagger_rails/application/index.html.erb +++ b/app/views/grape_swagger_rails/application/index.html.erb @@ -22,7 +22,7 @@ // And this resolution is to add header before initialize `SwaggerUI` // Some discussions: https://groups.google.com/forum/#!topic/swagger-swaggersocket/tJ0YHdTnBRk // https://github.com/swagger-api/swagger-ui/issues/1171#issuecomment-100374999 - <%= raw "headers.header_#{GrapeSwaggerRails.options.headers.count} = new SwaggerClient.ApiKeyAuthorization(options.api_key_name, prepareApiKeyAuthorization(), options.api_key_type);" unless GrapeSwaggerRails.options.headers.keys.include?('Authorization') %> + <%#= raw "headers.header_#{GrapeSwaggerRails.options.headers.count} = new SwaggerClient.ApiKeyAuthorization(options.api_key_name, prepareApiKeyAuthorization(), options.api_key_type);" unless GrapeSwaggerRails.options.headers.keys.include?('Authorization') %> window.swaggerUi = new SwaggerUi({ url: options.app_url + options.url, diff --git a/spec/dummy/app/api/api.rb b/spec/dummy/app/api/api.rb index 1547f72..07a864c 100644 --- a/spec/dummy/app/api/api.rb +++ b/spec/dummy/app/api/api.rb @@ -26,5 +26,6 @@ class API < Grape::API request.params.as_json end + mount V1::Resources::Animals add_swagger_documentation end diff --git a/spec/dummy/app/api/v1/resources/animals.rb b/spec/dummy/app/api/v1/resources/animals.rb new file mode 100644 index 0000000..b533f90 --- /dev/null +++ b/spec/dummy/app/api/v1/resources/animals.rb @@ -0,0 +1,11 @@ +module V1 + module Resources + class Animals < Grape::API + namespace :animals do + get do + [{ id: 1, name: 'Foo' }] + end + end + end + end +end From c23491f4f63180aa5391405ec6431ed76d298a3f Mon Sep 17 00:00:00 2001 From: mur-wtag Date: Mon, 3 Oct 2016 17:07:56 +0600 Subject: [PATCH 3/6] - spec will be failed CORE-826 --- spec/dummy/app/api/v1/resources/animals.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/dummy/app/api/v1/resources/animals.rb b/spec/dummy/app/api/v1/resources/animals.rb index b533f90..22a588a 100644 --- a/spec/dummy/app/api/v1/resources/animals.rb +++ b/spec/dummy/app/api/v1/resources/animals.rb @@ -1,3 +1,4 @@ +# This is a API endpoint class for animals module V1 module Resources class Animals < Grape::API From a0815098fde4a0e52a4d57d66bd8036af1b7bee5 Mon Sep 17 00:00:00 2001 From: mur-wtag Date: Mon, 3 Oct 2016 17:12:23 +0600 Subject: [PATCH 4/6] - Top-level documentation offense fix CORE-826 --- spec/dummy/app/api/v1/resources/animals.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/dummy/app/api/v1/resources/animals.rb b/spec/dummy/app/api/v1/resources/animals.rb index 22a588a..4713a96 100644 --- a/spec/dummy/app/api/v1/resources/animals.rb +++ b/spec/dummy/app/api/v1/resources/animals.rb @@ -9,4 +9,4 @@ class Animals < Grape::API end end end -end +end # this class mounted in '/api.rb' From c727a4466319e792ff5f53171af1c405b195a744 Mon Sep 17 00:00:00 2001 From: mur-wtag Date: Mon, 3 Oct 2016 17:16:11 +0600 Subject: [PATCH 5/6] - Disabling documentation smelling CORE-826 --- .rubocop.yml | 3 +++ spec/dummy/app/api/v1/resources/animals.rb | 3 +-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 04b2216..c1656a0 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -3,3 +3,6 @@ AllCops: - vendor/**/* inherit_from: .rubocop_todo.yml + +Documentation: + Enabled: false diff --git a/spec/dummy/app/api/v1/resources/animals.rb b/spec/dummy/app/api/v1/resources/animals.rb index 4713a96..b533f90 100644 --- a/spec/dummy/app/api/v1/resources/animals.rb +++ b/spec/dummy/app/api/v1/resources/animals.rb @@ -1,4 +1,3 @@ -# This is a API endpoint class for animals module V1 module Resources class Animals < Grape::API @@ -9,4 +8,4 @@ class Animals < Grape::API end end end -end # this class mounted in '/api.rb' +end From e85cc7c9fa2003bbc12a86a1255ca71938b258b7 Mon Sep 17 00:00:00 2001 From: mur-wtag Date: Mon, 3 Oct 2016 17:23:58 +0600 Subject: [PATCH 6/6] - without fix CORE-826 --- .../application/index.html.erb | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/app/views/grape_swagger_rails/application/index.html.erb b/app/views/grape_swagger_rails/application/index.html.erb index 8e068ee..676471d 100644 --- a/app/views/grape_swagger_rails/application/index.html.erb +++ b/app/views/grape_swagger_rails/application/index.html.erb @@ -15,15 +15,6 @@ <%=raw "headers.header_#{index} = new SwaggerClient.ApiKeyAuthorization('#{CGI.escapeHTML(key)}', '#{CGI.escapeHTML(value)}', 'header');" %> <% end %> - // In very next line we are going to initialize `SwaggerUi`. But To initialize `SwaggerUi` we need to have authorization header. - // And the inside `onComplete` we are adding `Authorization Header` by calling `addApiKeyAuthorization()`. - // So, initialization of `SwaggerUi` fails and triggers `onFaiulre` :p - - // And this resolution is to add header before initialize `SwaggerUI` - // Some discussions: https://groups.google.com/forum/#!topic/swagger-swaggersocket/tJ0YHdTnBRk - // https://github.com/swagger-api/swagger-ui/issues/1171#issuecomment-100374999 - <%#= raw "headers.header_#{GrapeSwaggerRails.options.headers.count} = new SwaggerClient.ApiKeyAuthorization(options.api_key_name, prepareApiKeyAuthorization(), options.api_key_type);" unless GrapeSwaggerRails.options.headers.keys.include?('Authorization') %> - window.swaggerUi = new SwaggerUi({ url: options.app_url + options.url, dom_id: "swagger-ui-container", @@ -51,12 +42,6 @@ }); function addApiKeyAuthorization() { - var api_key = $('#input_apiKey')[0].value; - if (api_key && api_key.trim() != "") - window.swaggerUi.api.clientAuthorizations.add("key", new SwaggerClient.ApiKeyAuthorization(options.api_key_name, prepareApiKeyAuthorization(), options.api_key_type)); - } - - function prepareApiKeyAuthorization() { var key = $('#input_apiKey')[0].value; if (key && key.trim() != "") { @@ -65,9 +50,9 @@ } else if (options.api_auth == 'bearer') { key = "Bearer " + key } + window.swaggerUi.api.clientAuthorizations.add("key", new SwaggerClient.ApiKeyAuthorization(options.api_key_name, key, options.api_key_type)); } - return key; - } + } $('#input_apiKey').change(addApiKeyAuthorization);