Skip to content

Commit 927629e

Browse files
committed
Merge pull request #275 from walski/fix-86
Fixes #86
2 parents d85c2b3 + e3c94ba commit 927629e

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

CHANGELOG.markdown

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

44
* [#265](https://github.com/intridea/grape/issues/264): Fix: The class ValidationError should be in the module "Grape::Exceptions". Fixes [#264](https://github.com/intridea/grape/issues/264) - [@thepumpkin1979](https://github.com/thepumpkin1979).
55
* [#269](https://github.com/intridea/grape/pull/269): Fix: LocalJumpError will not be raised when using explict return in API methods - [@simulacre](https://github.com/simulacre)
6+
* [#86] (https://github.com/intridea/grape/issues/275): Fix Path based versioning not recognizing '/' route
67
* Your contribution here.
78

89
0.2.2

lib/grape/endpoint.rb

+14-4
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,20 @@ def prepare_routes
110110
def prepare_path(path)
111111
parts = []
112112
parts << settings[:root_prefix] if settings[:root_prefix]
113-
parts << ':version' if settings[:version] && settings[:version_options][:using] == :path
114-
parts << namespace.to_s if namespace
115-
parts << path.to_s if path && '/' != path
116-
Rack::Mount::Utils.normalize_path(parts.join('/') + '(.:format)')
113+
114+
uses_path_versioning = settings[:version] && settings[:version_options][:using] == :path
115+
namespace_is_empty = namespace && (namespace.to_s =~ /^\s*$/ || namespace.to_s == '/')
116+
path_is_empty = path && (path.to_s =~ /^\s*$/ || path.to_s == '/')
117+
118+
parts << ':version' if uses_path_versioning
119+
if !uses_path_versioning || (!namespace_is_empty || !path_is_empty)
120+
parts << namespace.to_s if namespace
121+
parts << path.to_s if path && '/' != path
122+
format_suffix = '(.:format)'
123+
else
124+
format_suffix = '(/.:format)'
125+
end
126+
Rack::Mount::Utils.normalize_path(parts.join('/') + format_suffix)
117127
end
118128

119129
def namespace

spec/grape/api_spec.rb

+47
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,53 @@ def app; subject end
189189
last_response.body.should eql 'Created a Vote'
190190
end
191191

192+
describe "root routes should work with" do
193+
before do
194+
def subject.enable_root_route!
195+
self.get("/") {"root"}
196+
end
197+
end
198+
199+
after do
200+
last_response.body.should eql 'root'
201+
end
202+
203+
describe "path versioned APIs" do
204+
before do
205+
subject.version 'v1', :using => :path
206+
subject.enable_root_route!
207+
end
208+
209+
it "without a format" do
210+
versioned_get "/", "v1", :using => :path
211+
end
212+
213+
it "with a format" do
214+
get "/v1/.json"
215+
end
216+
end
217+
218+
it "header versioned APIs" do
219+
subject.version 'v1', :using => :header, :vendor => 'test'
220+
subject.enable_root_route!
221+
222+
versioned_get "/", "v1", :using => :header
223+
end
224+
225+
it "param versioned APIs" do
226+
subject.version 'v1', :using => :param
227+
subject.enable_root_route!
228+
229+
versioned_get "/", "v1", :using => :param
230+
end
231+
232+
it "unversioned APIs" do
233+
subject.enable_root_route!
234+
235+
get "/"
236+
end
237+
end
238+
192239
it 'should allow for multiple paths' do
193240
subject.get(["/abc", "/def"]) do
194241
"foo"

0 commit comments

Comments
 (0)