Skip to content

Commit d7cdbd4

Browse files
committed
Improvements to parse_path
- parse_path now more closely matches that of Rack::Mount::Strexp in the way that it parses variables from the paths. This allows grape-swagger to generate proper documentation.
1 parent e2336f3 commit d7cdbd4

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
@@ -152,7 +152,12 @@ def parse_header_params(params)
152152
def parse_path(path, version)
153153
# adapt format to swagger format
154154
parsed_path = path.gsub('(.:format)', '.{format}')
155-
parsed_path = parsed_path.gsub(/:([a-z]+)/, '{\1}')
155+
# This is attempting to emulate the behavior of
156+
# Rack::Mount::Strexp. We cannot use Strexp directly because
157+
# all it does is generate regular expressions for parsing URLs.
158+
# TODO: Implement a Racc tokenizer to properly generate the
159+
# parsed path.
160+
parsed_path = parsed_path.gsub(/:([a-zA-Z_]\w*)/, '{\1}')
156161
# add the version
157162
parsed_path = parsed_path.gsub('{version}', version) if version
158163
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)