From 3bcfc2f47b2993b9c57d21eb3b977c2310352af6 Mon Sep 17 00:00:00 2001 From: Pablo Bendersky Date: Tue, 26 Jul 2016 13:41:19 -0300 Subject: [PATCH 1/6] added support for nicknamed routes --- lib/grape-swagger/endpoint.rb | 8 ++++++- spec/swagger_v2/nicknamed_api_spec.rb | 30 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 spec/swagger_v2/nicknamed_api_spec.rb diff --git a/lib/grape-swagger/endpoint.rb b/lib/grape-swagger/endpoint.rb index eb6022ea..43e160c6 100644 --- a/lib/grape-swagger/endpoint.rb +++ b/lib/grape-swagger/endpoint.rb @@ -110,7 +110,7 @@ def method_object(route, options, path) method[:parameters] = params_object(route) method[:responses] = response_object(route, options[:markdown]) method[:tags] = tag_object(route) - method[:operationId] = GrapeSwagger::DocMethods::OperationId.build(route, path) + method[:operationId] = operation_id(route, path) method.delete_if { |_, value| value.blank? } [route.request_method.downcase.to_sym, method] @@ -133,6 +133,12 @@ def description_object(route, markdown) description end + + def operation_id(route, path) + operation_id = route.options[:nickname] || GrapeSwagger::DocMethods::OperationId.build(route, path) + + operation_id + end def produces_object(route, format) mime_types = GrapeSwagger::DocMethods::ProducesConsumes.call(format) diff --git a/spec/swagger_v2/nicknamed_api_spec.rb b/spec/swagger_v2/nicknamed_api_spec.rb new file mode 100644 index 00000000..56755718 --- /dev/null +++ b/spec/swagger_v2/nicknamed_api_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +describe 'a nicknamed mounted api' do + before :all do + class NicknamedMountedApi < Grape::API + desc 'Show this endpoint', nickname: 'simple' + get '/simple' do + { foo: 'bar' } + end + end + + class NicknamedApi < Grape::API + mount NicknamedMountedApi + add_swagger_documentation + end + end + + def app + NicknamedApi + end + + subject do + get '/swagger_doc.json' + JSON.parse(last_response.body) + end + + it "uses the nickname as the operationId" do + expect(subject['paths']['/simple']['get']['operationId']).to eql('simple') + end +end From 859b6f4be14583ddc8e37502fd394ddb5e77df6a Mon Sep 17 00:00:00 2001 From: Pablo Bendersky Date: Tue, 26 Jul 2016 13:54:52 -0300 Subject: [PATCH 2/6] uses let instead of method to instantiate the app --- spec/swagger_v2/nicknamed_api_spec.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/spec/swagger_v2/nicknamed_api_spec.rb b/spec/swagger_v2/nicknamed_api_spec.rb index 56755718..75f992dc 100644 --- a/spec/swagger_v2/nicknamed_api_spec.rb +++ b/spec/swagger_v2/nicknamed_api_spec.rb @@ -15,9 +15,7 @@ class NicknamedApi < Grape::API end end - def app - NicknamedApi - end + let(:app) { NicknamedApi } subject do get '/swagger_doc.json' From a6bf250e2febecd8f2a82245884e27ed34a20089 Mon Sep 17 00:00:00 2001 From: Pablo Bendersky Date: Tue, 26 Jul 2016 14:05:42 -0300 Subject: [PATCH 3/6] updated spec to use anon class --- spec/swagger_v2/nicknamed_api_spec.rb | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/spec/swagger_v2/nicknamed_api_spec.rb b/spec/swagger_v2/nicknamed_api_spec.rb index 75f992dc..b614c8f3 100644 --- a/spec/swagger_v2/nicknamed_api_spec.rb +++ b/spec/swagger_v2/nicknamed_api_spec.rb @@ -1,22 +1,17 @@ require 'spec_helper' describe 'a nicknamed mounted api' do - before :all do - class NicknamedMountedApi < Grape::API + def app + Class.new(Grape::API) do desc 'Show this endpoint', nickname: 'simple' get '/simple' do { foo: 'bar' } end - end - - class NicknamedApi < Grape::API - mount NicknamedMountedApi - add_swagger_documentation + + add_swagger_documentation format: :json end end - let(:app) { NicknamedApi } - subject do get '/swagger_doc.json' JSON.parse(last_response.body) From f5c006502d290b8225cc92becd6ea57946c90961 Mon Sep 17 00:00:00 2001 From: Pablo Bendersky Date: Tue, 26 Jul 2016 15:38:40 -0300 Subject: [PATCH 4/6] fixed rubocop errors --- spec/swagger_v2/nicknamed_api_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/swagger_v2/nicknamed_api_spec.rb b/spec/swagger_v2/nicknamed_api_spec.rb index b614c8f3..f46185a9 100644 --- a/spec/swagger_v2/nicknamed_api_spec.rb +++ b/spec/swagger_v2/nicknamed_api_spec.rb @@ -7,7 +7,7 @@ def app get '/simple' do { foo: 'bar' } end - + add_swagger_documentation format: :json end end @@ -17,7 +17,7 @@ def app JSON.parse(last_response.body) end - it "uses the nickname as the operationId" do + it 'uses the nickname as the operationId' do expect(subject['paths']['/simple']['get']['operationId']).to eql('simple') end end From 58effb36077ea0b736f5948ac733daaedd25ed5e Mon Sep 17 00:00:00 2001 From: Pablo Bendersky Date: Tue, 26 Jul 2016 17:05:59 -0300 Subject: [PATCH 5/6] Refactor to fix rubocop issues. --- lib/grape-swagger/doc_methods/operation_id.rb | 12 +++++++++--- lib/grape-swagger/endpoint.rb | 8 +------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/grape-swagger/doc_methods/operation_id.rb b/lib/grape-swagger/doc_methods/operation_id.rb index a1872a8b..0134ca4d 100644 --- a/lib/grape-swagger/doc_methods/operation_id.rb +++ b/lib/grape-swagger/doc_methods/operation_id.rb @@ -3,11 +3,17 @@ module DocMethods class OperationId class << self def build(route, path = nil) - verb = route.request_method.to_s.downcase + if route.options[:nickname] + operation_id = route.options[:nickname] + else + verb = route.request_method.to_s.downcase - operation = manipulate(path) unless path.nil? + operation = manipulate(path) unless path.nil? - "#{verb}#{operation}" + operation_id = "#{verb}#{operation}" + end + + operation_id end def manipulate(path) diff --git a/lib/grape-swagger/endpoint.rb b/lib/grape-swagger/endpoint.rb index 43e160c6..eb6022ea 100644 --- a/lib/grape-swagger/endpoint.rb +++ b/lib/grape-swagger/endpoint.rb @@ -110,7 +110,7 @@ def method_object(route, options, path) method[:parameters] = params_object(route) method[:responses] = response_object(route, options[:markdown]) method[:tags] = tag_object(route) - method[:operationId] = operation_id(route, path) + method[:operationId] = GrapeSwagger::DocMethods::OperationId.build(route, path) method.delete_if { |_, value| value.blank? } [route.request_method.downcase.to_sym, method] @@ -133,12 +133,6 @@ def description_object(route, markdown) description end - - def operation_id(route, path) - operation_id = route.options[:nickname] || GrapeSwagger::DocMethods::OperationId.build(route, path) - - operation_id - end def produces_object(route, format) mime_types = GrapeSwagger::DocMethods::ProducesConsumes.call(format) From de7ad8f8f4a894d43ac2ddd1cb2704e74725d8ef Mon Sep 17 00:00:00 2001 From: Pablo Bendersky Date: Tue, 26 Jul 2016 17:21:45 -0300 Subject: [PATCH 6/6] Added entry in Changelog. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e5907e1..df6496e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * [#476](https://github.com/ruby-grape/grape-swagger/pull/476): Fixes for handling the parameter type when body parameters are defined inside desc block - [@anakinj](https://github.com/anakinj). * [#478](https://github.com/ruby-grape/grape-swagger/pull/478): Refactors building of properties, corrects documentation of array items - [@LeFnord](https://github.com/LeFnord). * [#479](https://github.com/ruby-grape/grape-swagger/pull/479): Fix regex for Array and Multi Type in doc_methods. Parsing of "[APoint]" should return "APoint" - [@frodrigo](https://github.com/frodrigo). +* [#483](https://github.com/ruby-grape/grape-swagger/pull/483): Added support for nicknamed routes - [@pbendersky](https://github.com/pbendersky) * Your contribution here. ### 0.22.0 (July 12, 2016)