Skip to content

Commit 832fb0a

Browse files
committed
Really simplistic version support.
1 parent 15ab62e commit 832fb0a

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

lib/grape-swagger.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def self.setup(options)
8181
routes_array = routes.map do |route|
8282
notes = route.route_notes && @@markdown ? Kramdown::Document.new(route.route_notes.strip_heredoc).to_html : route.route_notes
8383
{
84-
:path => parse_path(route.route_path),
84+
:path => parse_path(route.route_path, api_version),
8585
:operations => [{
8686
:notes => notes,
8787
:summary => route.route_description || '',
@@ -122,11 +122,13 @@ def parse_params(params, path, method)
122122
end
123123
end
124124

125-
def parse_path(path)
125+
def parse_path(path, version)
126126
# adapt format to swagger format
127127
parsed_path = path.gsub('(.:format)', '.{format}')
128-
# adapt params to swagger format
129-
parsed_path.gsub(/:([a-z]+)/, '{\1}')
128+
parsed_path = parsed_path.gsub(/:([a-z]+)/, '{\1}')
129+
# add the version
130+
parsed_path = parsed_path.gsub('{version}', version) if version
131+
parsed_path
130132
end
131133
end
132134
end

spec/non_default_api_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,32 @@ def app; SimpleApiWithMarkdown end
120120
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=>[]}]}]}"
121121
end
122122
end
123+
124+
context "versioned API" do
125+
before(:all) do
126+
class VersionedMountedApi < Grape::API
127+
prefix 'api'
128+
version 'v1'
129+
130+
desc 'this gets something'
131+
get '/something' do
132+
{:bla => 'something'}
133+
end
134+
end
135+
136+
class SimpleApiWithVersion < Grape::API
137+
mount VersionedMountedApi
138+
add_swagger_documentation :api_version => "v1"
139+
end
140+
end
141+
142+
def app; SimpleApiWithVersion end
143+
144+
it "parses version and places it in the path" do
145+
get '/swagger_doc/api'
146+
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=>[]}]}]}"
147+
end
148+
end
149+
123150

124151
end

0 commit comments

Comments
 (0)