Skip to content

Commit 23454cf

Browse files
author
Tim Vandecasteele
committed
Fix bug where mounting in a versioned api by path was not possible.
1 parent 4771e76 commit 23454cf

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

lib/grape-swagger.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ def self.setup(options)
6464
routes.reject!{ |route, value| "/#{route}/".index(parse_path(@@mount_path, nil) << '/') == 0 }
6565
end
6666

67-
routes_array = routes.keys.map do |route|
68-
{ :path => "#{@@mount_path}/#{route}.{format}" }
67+
routes_array = routes.keys.map do |local_route|
68+
{ :path => "#{parse_path(route.route_path.gsub('(.:format)', ''),route.route_version)}/#{local_route}.{format}" }
6969
end
7070
{
7171
apiVersion: api_version,
@@ -158,10 +158,10 @@ def parse_header_params(params)
158158
def parse_path(path, version)
159159
# adapt format to swagger format
160160
parsed_path = path.gsub('(.:format)', '.{format}')
161-
# This is attempting to emulate the behavior of
162-
# Rack::Mount::Strexp. We cannot use Strexp directly because
163-
# all it does is generate regular expressions for parsing URLs.
164-
# TODO: Implement a Racc tokenizer to properly generate the
161+
# This is attempting to emulate the behavior of
162+
# Rack::Mount::Strexp. We cannot use Strexp directly because
163+
# all it does is generate regular expressions for parsing URLs.
164+
# TODO: Implement a Racc tokenizer to properly generate the
165165
# parsed path.
166166
parsed_path = parsed_path.gsub(/:([a-zA-Z_]\w*)/, '{\1}')
167167
# add the version

spec/non_default_api_spec.rb

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,38 @@ def app; SimpleApiWithApiVersion end
6161
end
6262
end
6363

64+
context "mounting in a versioned api" do
65+
before(:all) do
66+
class SimpleApiToMountInVersionedApi < Grape::API
67+
desc 'this gets something'
68+
get '/something' do
69+
{:bla => 'something'}
70+
end
71+
end
72+
73+
class SimpleApiWithVersionInPath < Grape::API
74+
version 'v1', :using => :path
75+
76+
mount SimpleApiToMountInVersionedApi
77+
add_swagger_documentation
78+
end
79+
end
80+
81+
def app; SimpleApiWithVersionInPath end
82+
83+
it "should get the documentation on a versioned path /v1/swagger_doc" do
84+
get '/v1/swagger_doc'
85+
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :operations=>[], :apis=>[{:path=>\"/v1/swagger_doc/something.{format}\"}, {:path=>\"/v1/swagger_doc/swagger_doc.{format}\"}]}"
86+
end
87+
88+
it "should get the resource specific documentation on a versioned path /v1/swagger_doc/something" do
89+
get '/v1/swagger_doc/something'
90+
last_response.status.should == 200
91+
end
92+
93+
end
94+
95+
6496
context "overruling hiding the documentation paths" do
6597
before(:all) do
6698
class HideDocumentationPathMountedApi < Grape::API
@@ -80,7 +112,7 @@ def app; SimpleApiWithHiddenDocumentation end
80112

81113
it "it doesn't show the documentation path on /swagger_doc" do
82114
get '/swagger_doc'
83-
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :operations=>[], :apis=>[{:path=>\"/swagger_doc/something.{format}\"}]}"
115+
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :operations=>[], :apis=>[{:path=>\"/swagger_doc/something.{format}\"}]}"
84116
end
85117
end
86118

@@ -143,27 +175,27 @@ def app; SimpleApiWithMarkdown end
143175
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :resourcePath=>\"\", :apis=>[{:path=>\"/something.{format}\", :operations=>[{:notes=>\"<p><em>test</em></p>\\n\", :summary=>\"this gets something\", :nickname=>\"GET-something---format-\", :httpMethod=>\"GET\", :parameters=>[]}]}]}"
144176
end
145177
end
146-
178+
147179
context "versioned API" do
148180
before(:all) do
149181
class VersionedMountedApi < Grape::API
150182
prefix 'api'
151183
version 'v1'
152-
184+
153185
desc 'this gets something'
154186
get '/something' do
155187
{:bla => 'something'}
156188
end
157189
end
158-
190+
159191
class SimpleApiWithVersion < Grape::API
160192
mount VersionedMountedApi
161193
add_swagger_documentation :api_version => "v1"
162194
end
163195
end
164-
196+
165197
def app; SimpleApiWithVersion end
166-
198+
167199
it "parses version and places it in the path" do
168200
get '/swagger_doc/api'
169201
last_response.body.should == "{:apiVersion=>\"v1\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :resourcePath=>\"\", :apis=>[{:path=>\"/api/v1/something.{format}\", :operations=>[{:notes=>nil, :summary=>\"this gets something\", :nickname=>\"GET-api--version-something---format-\", :httpMethod=>\"GET\", :parameters=>[]}]}]}"

0 commit comments

Comments
 (0)