Skip to content

Commit e3094fa

Browse files
Merge pull request #17 from justfalter/master
Improvements to parse_path
2 parents 5015d44 + d7cdbd4 commit e3094fa

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/grape-swagger.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,12 @@ 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-
parsed_path = parsed_path.gsub(/:([a-z]+)/, '{\1}')
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
165+
# parsed path.
166+
parsed_path = parsed_path.gsub(/:([a-zA-Z_]\w*)/, '{\1}')
162167
# add the version
163168
parsed_path = parsed_path.gsub('{version}', version) if version
164169
parsed_path

spec/grape-swagger-helper_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ class HelperTestAPI < Grape::API
5151
@api.parse_path(path, nil).should == "{abc}/def.{format}"
5252
end
5353

54+
it "should parse a path that has vars with underscores in the name" do
55+
path = "abc/:def_g(.:format)"
56+
@api.parse_path(path, nil).should == "abc/{def_g}.{format}"
57+
58+
end
59+
60+
it "should parse a path that has vars with numbers in the name" do
61+
path = "abc/:sha1(.:format)"
62+
@api.parse_path(path, nil).should == "abc/{sha1}.{format}"
63+
end
64+
65+
it "should parse a path that has multiple variables" do
66+
path1 = "abc/:def/:geh(.:format)"
67+
path2 = "abc/:def:geh(.:format)"
68+
@api.parse_path(path1, nil).should == "abc/{def}/{geh}.{format}"
69+
@api.parse_path(path2, nil).should == "abc/{def}{geh}.{format}"
70+
end
71+
5472
it "should parse the path with a specified version" do
5573
path = ":abc/{version}/def(.:format)"
5674
@api.parse_path(path, 'v1').should == "{abc}/v1/def.{format}"

0 commit comments

Comments
 (0)