Skip to content

Commit

Permalink
Issue 508 bug in combine routes (#529)
Browse files Browse the repository at this point in the history
* - Simple dirty patch to make it work

- CHANGELOG changed

- Spec added

- Fixing specs

- Fixing specs

* makes rubocop happy
- updates changelog
  • Loading branch information
peter scholz authored Nov 16, 2016
1 parent a09d7e1 commit 5cdcaaa
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#### Fixes

* [#509](https://github.com/ruby-grape/grape-swagger/pull/509), [#529](https://github.com/ruby-grape/grape-swagger/pull/529): Making parent-less routes working - [@contributor](https://github.com/mur-wtag).
* Your contribution here.

### 0.25.0 (October 31, 2016)
Expand Down
8 changes: 7 additions & 1 deletion lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def combine_namespace_routes(namespaces)
# iterate over each single namespace
namespaces.each do |name, namespace|
# get the parent route for the namespace
parent_route_name = name.match(%r{^/?([^/]*).*$})[1]
parent_route_name = extract_parent_route(name)
parent_route = @target_class.combined_routes[parent_route_name]
# fetch all routes that are within the current namespace
namespace_routes = parent_route.reject do |route|
Expand Down Expand Up @@ -142,6 +142,12 @@ def combine_namespace_routes(namespaces)
end
end

def extract_parent_route(name)
route_name = name.match(%r{^/?([^/]*).*$})[1]
return route_name unless route_name.include? ':'
name.match(/\/[a-z]+/)[0].delete('/')
end

def sub_routes_from(parent_route, sub_namespaces)
sub_ns_paths = sub_namespaces.collect { |ns_name, _| ["/#{ns_name}", "/:version/#{ns_name}"] }
sub_routes = parent_route.reject do |route|
Expand Down
17 changes: 17 additions & 0 deletions spec/support/namespace_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ class NamespaceApi < Grape::API
end
end
end

class ParentLessNamespaceApi < Grape::API
route_param :animal do
route_param :breed do
resource :queues do
route_param :queue_id do
resource :reservations do
desc 'Lists all reservations specific type of animal of specific breed in specific queue'
get do
{ bla: 'Bla Black' }
end
end
end
end
end
end
end
end
end
end
47 changes: 47 additions & 0 deletions spec/swagger_v2/parent_less_namespace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'spec_helper'

describe 'a parent less namespace' do
include_context 'namespace example'

before :all do
class ParentLessApi < Grape::API
prefix :api
mount TheApi::ParentLessNamespaceApi
add_swagger_documentation version: 'v1'
end
end

def app
ParentLessApi
end

describe 'retrieves swagger-documentation on /swagger_doc' do
let(:route_name) { ':animal/:breed/queues/:queue_id/reservations' }
subject do
get '/api/swagger_doc.json'
JSON.parse(last_response.body)
end

context 'not raises error' do
specify do
expect(subject['tags']).to eql([{ 'name' => 'queues', 'description' => 'Operations about queues' }])
expect(subject['paths']['/api/{animal}/{breed}/queues/{queue_id}/reservations']['get']['operationId'])
.to eql('getApiAnimalBreedQueuesQueueIdReservations')
end
end

context 'raises error' do
specify do
allow_any_instance_of(ParentLessApi)
.to receive(:extract_parent_route).with(route_name).and_return(':animal') # BUT IT'S NOT STUBBING, CAUSE IT'S A PRIVATE METHODS
expect { subject }.to raise_error NoMethodError
end
end

context 'ParentLessApi.extract_parent_route' do
specify do
expect(ParentLessApi.send(:extract_parent_route, route_name)).to eq('queues')
end
end
end
end

0 comments on commit 5cdcaaa

Please sign in to comment.