Skip to content

Commit

Permalink
Combine namespaces with their mount_path to allow APIs with specified…
Browse files Browse the repository at this point in the history
… mount_paths (ruby-grape#505)

i.e. `mount SomeAPI => '/some_api'`
  • Loading branch information
KevinLiddle authored and LeFnord committed Sep 22, 2016
1 parent a385eeb commit 35bf741
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions spec/swagger_v2/namespaced_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 35bf741

Please sign in to comment.