Skip to content

Commit f70bcbc

Browse files
committed
Handle cases where a route's prefix is a nested URL
Nested prefixes (any prefix containing a '/' character) prior to this commit would incorrectly get tagged as the first segment of the URL prefix. E.g. a route like "api/beta/v1/patients" would incorrectly get tagged as "api" instead of "patients" Added a nested prefix to the namespace_tags prefix RSpec -- this example breaks without the changes to endpoint.rb.
1 parent 445d2d4 commit f70bcbc

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#### Fixes
88

9-
* Your contribution here.
9+
* [#758](https://github.com/ruby-grape/grape-swagger/pull/758): Handle cases where a route's prefix is a nested URL - [@SimonKaluza](https://github.com/simonkaluza).
1010

1111
### 0.33.0 (June 21, 2019)
1212

lib/grape-swagger/endpoint.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,10 @@ def success_codes_from_route(route)
251251
def tag_object(route, path)
252252
version = GrapeSwagger::DocMethods::Version.get(route)
253253
version = [version] unless version.is_a?(Array)
254-
254+
prefix = route.prefix.to_s.split('/').reject(&:empty?)
255255
Array(
256256
path.split('{')[0].split('/').reject(&:empty?).delete_if do |i|
257-
i == route.prefix.to_s || version.map(&:to_s).include?(i)
257+
prefix.include?(i) || version.map(&:to_s).include?(i)
258258
end.first
259259
).presence
260260
end

spec/swagger_v2/namespace_tags_prefix_spec.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,25 @@ class CascadingVersionApi < Grape::API
2929
end
3030
end
3131
end
32+
33+
class NestedPrefixApi < Grape::API
34+
version :v1
35+
prefix 'nested_prefix/api'
36+
37+
namespace :deep_namespace do
38+
desc 'This gets something within a deeply nested resource'
39+
get '/deep' do
40+
{ bla: 'something' }
41+
end
42+
end
43+
end
3244
end
3345

3446
class TagApi < Grape::API
3547
prefix :api
3648
mount TheApi::CascadingVersionApi
3749
mount TheApi::NamespaceApi
50+
mount TheApi::NestedPrefixApi
3851
add_swagger_documentation
3952
end
4053
end
@@ -55,7 +68,8 @@ def app
5568
{ 'name' => 'hudson', 'description' => 'Operations about hudsons' },
5669
{ 'name' => 'colorado', 'description' => 'Operations about colorados' },
5770
{ 'name' => 'thames', 'description' => 'Operations about thames' },
58-
{ 'name' => 'niles', 'description' => 'Operations about niles' }
71+
{ 'name' => 'niles', 'description' => 'Operations about niles' },
72+
{ 'name' => 'deep_namespace', 'description' => 'Operations about deep_namespaces' }
5973
]
6074
)
6175

0 commit comments

Comments
 (0)