Skip to content

Issue 508 bug in combine routes #529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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