diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c3b430e..2816a47f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ * [#503](https://github.com/ruby-grape/grape-swagger/pull/503): Corrects exposing of inline definitions - [@LeFnord](https://github.com/LeFnord). * [#494](https://github.com/ruby-grape/grape-swagger/pull/494): Header parametes are now included in documentation when body parameters have been defined - [@anakinj](https://github.com/anakinj). +* [#505](https://github.com/ruby-grape/grape-swagger/pull/505): Combines namespaces with their mounted paths to allow APIs with specified mount_paths - [@KevinLiddle](https://github.com/KevinLiddle). * Your contribution here. ### 0.23.0 (August 5, 2016) diff --git a/lib/grape-swagger.rb b/lib/grape-swagger.rb index a43f38f4..e3f7813d 100644 --- a/lib/grape-swagger.rb +++ b/lib/grape-swagger.rb @@ -86,7 +86,9 @@ def combine_namespaces(app) end # use the full namespace here (not the latest level only) # and strip leading slash - @target_class.combined_namespaces[endpoint.namespace.sub(/^\//, '')] = ns if ns + mount_path = (endpoint.namespace_stackable(:mount_path) || []).join('/') + full_namespace = (mount_path + endpoint.namespace).sub(/\/{2,}/, '/').sub(/^\//, '') + @target_class.combined_namespaces[full_namespace] = ns if ns combine_namespaces(endpoint.options[:app]) if endpoint.options[:app] end diff --git a/spec/swagger_v2/namespaced_api_spec.rb b/spec/swagger_v2/namespaced_api_spec.rb index ad7fab36..3297a9ec 100644 --- a/spec/swagger_v2/namespaced_api_spec.rb +++ b/spec/swagger_v2/namespaced_api_spec.rb @@ -64,4 +64,56 @@ def app expect(subject['description']).to eql('Description for aspace') end end + + context 'mounted under a route' do + def app + namespaced_api = Class.new(Grape::API) do + namespace :bspace do + get '/', desc: 'Description for aspace' + end + end + + Class.new(Grape::API) do + mount namespaced_api => '/mounted' + add_swagger_documentation format: :json + end + end + + subject do + get '/swagger_doc' + JSON.parse(last_response.body)['paths']['/mounted/bspace']['get'] + end + + it 'shows the namespace description in the json spec' do + expect(subject['description']).to eql('Description for aspace') + end + end + + context 'arbitrary mounting' do + def app + inner_namespaced_api = Class.new(Grape::API) do + namespace :bspace do + get '/', desc: 'Description for aspace' + end + end + + outer_namespaced_api = Class.new(Grape::API) do + mount inner_namespaced_api => '/mounted' + end + + Class.new(Grape::API) do + mount outer_namespaced_api => '/' + add_swagger_documentation format: :json + end + end + + subject do + get '/swagger_doc' + JSON.parse(last_response.body)['paths']['/mounted/bspace']['get'] + end + + it 'shows the namespace description in the json spec' do + expect(subject['description']).to eql('Description for aspace') + end + end end