Skip to content

Commit dbcb1dc

Browse files
committed
allow multiple definitions of the same endpoint under different versions
1 parent dd75477 commit dbcb1dc

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

lib/grape/api.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,12 @@ def prefix(prefix = nil)
6262
# end
6363
#
6464
# version 'v1' do
65-
# get '/legacy' do
65+
# get '/main' do
6666
# {:legacy => 'data'}
6767
# end
6868
# end
6969
# end
70+
#
7071
def version(*new_versions, &block)
7172
new_versions.any? ? nest(block){ set(:version, new_versions) } : settings[:version]
7273
end
@@ -139,10 +140,12 @@ def route(methods, paths, &block)
139140
paths = ['/'] if paths == []
140141
paths = Array(paths)
141142
endpoint = build_endpoint(&block)
143+
options = {}
144+
options[:version] = /#{version.join('|')}/ if version
142145

143146
methods.each do |method|
144147
paths.each do |path|
145-
path = Rack::Mount::Strexp.compile(compile_path(path))
148+
path = Rack::Mount::Strexp.compile(compile_path(path), options)
146149
route_set.add_route(endpoint,
147150
:path_info => path,
148151
:request_method => (method.to_s.upcase unless method == :any)

spec/grape/api_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,27 @@ def app; subject end
7878
get '/v3/awesome'
7979
last_response.status.should == 404
8080
end
81+
82+
it 'should allow the same endpoint to be implemented for different versions' do
83+
subject.version 'v2'
84+
subject.get 'version' do
85+
request.env['api.version']
86+
end
87+
88+
subject.version 'v1' do
89+
get 'version' do
90+
"version " + request.env['api.version']
91+
end
92+
end
93+
94+
get '/v2/version'
95+
last_response.status.should == 200
96+
last_response.body.should == 'v2'
97+
get '/v1/version'
98+
last_response.status.should == 200
99+
last_response.body.should == 'version v1'
100+
end
101+
81102
end
82103

83104
describe '.namespace' do

0 commit comments

Comments
 (0)