Skip to content

Commit 5cdcaaa

Browse files
author
peter scholz
authored
Issue 508 bug in combine routes (#529)
* - Simple dirty patch to make it work - CHANGELOG changed - Spec added - Fixing specs - Fixing specs * makes rubocop happy - updates changelog
1 parent a09d7e1 commit 5cdcaaa

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#### Fixes
88

9+
* [#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).
910
* Your contribution here.
1011

1112
### 0.25.0 (October 31, 2016)

lib/grape-swagger.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def combine_namespace_routes(namespaces)
100100
# iterate over each single namespace
101101
namespaces.each do |name, namespace|
102102
# get the parent route for the namespace
103-
parent_route_name = name.match(%r{^/?([^/]*).*$})[1]
103+
parent_route_name = extract_parent_route(name)
104104
parent_route = @target_class.combined_routes[parent_route_name]
105105
# fetch all routes that are within the current namespace
106106
namespace_routes = parent_route.reject do |route|
@@ -142,6 +142,12 @@ def combine_namespace_routes(namespaces)
142142
end
143143
end
144144

145+
def extract_parent_route(name)
146+
route_name = name.match(%r{^/?([^/]*).*$})[1]
147+
return route_name unless route_name.include? ':'
148+
name.match(/\/[a-z]+/)[0].delete('/')
149+
end
150+
145151
def sub_routes_from(parent_route, sub_namespaces)
146152
sub_ns_paths = sub_namespaces.collect { |ns_name, _| ["/#{ns_name}", "/:version/#{ns_name}"] }
147153
sub_routes = parent_route.reject do |route|

spec/support/namespace_tags.rb

+17
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ class NamespaceApi < Grape::API
6666
end
6767
end
6868
end
69+
70+
class ParentLessNamespaceApi < Grape::API
71+
route_param :animal do
72+
route_param :breed do
73+
resource :queues do
74+
route_param :queue_id do
75+
resource :reservations do
76+
desc 'Lists all reservations specific type of animal of specific breed in specific queue'
77+
get do
78+
{ bla: 'Bla Black' }
79+
end
80+
end
81+
end
82+
end
83+
end
84+
end
85+
end
6986
end
7087
end
7188
end
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require 'spec_helper'
2+
3+
describe 'a parent less namespace' do
4+
include_context 'namespace example'
5+
6+
before :all do
7+
class ParentLessApi < Grape::API
8+
prefix :api
9+
mount TheApi::ParentLessNamespaceApi
10+
add_swagger_documentation version: 'v1'
11+
end
12+
end
13+
14+
def app
15+
ParentLessApi
16+
end
17+
18+
describe 'retrieves swagger-documentation on /swagger_doc' do
19+
let(:route_name) { ':animal/:breed/queues/:queue_id/reservations' }
20+
subject do
21+
get '/api/swagger_doc.json'
22+
JSON.parse(last_response.body)
23+
end
24+
25+
context 'not raises error' do
26+
specify do
27+
expect(subject['tags']).to eql([{ 'name' => 'queues', 'description' => 'Operations about queues' }])
28+
expect(subject['paths']['/api/{animal}/{breed}/queues/{queue_id}/reservations']['get']['operationId'])
29+
.to eql('getApiAnimalBreedQueuesQueueIdReservations')
30+
end
31+
end
32+
33+
context 'raises error' do
34+
specify do
35+
allow_any_instance_of(ParentLessApi)
36+
.to receive(:extract_parent_route).with(route_name).and_return(':animal') # BUT IT'S NOT STUBBING, CAUSE IT'S A PRIVATE METHODS
37+
expect { subject }.to raise_error NoMethodError
38+
end
39+
end
40+
41+
context 'ParentLessApi.extract_parent_route' do
42+
specify do
43+
expect(ParentLessApi.send(:extract_parent_route, route_name)).to eq('queues')
44+
end
45+
end
46+
end
47+
end

0 commit comments

Comments
 (0)