提交 572bbab2 编写于 作者: B brainopia

Improve shorthand matching for routes

Shorthand route match is when controller and action are taken literally from path.
E.g.
get '/foo/bar' # => will use 'foo#bar' as endpoint
get '/foo/bar/baz' # => will use 'foo/bar#baz' as endpoint

Not any path with level two or more of nesting can be used as shortcut.
If path contains any characters outside of /[\w-]/ then it can't be
used as such.

This commit ensures that invalid shortcuts aren't used.

':controller/:action/postfix' - is an example of invalid shortcut
that was previosly matched and led to exception:
"ArgumentError - ':controller/:action' is not a supported controller name"
上级 04d1c371
......@@ -1504,7 +1504,7 @@ def match(path, *rest)
end
def using_match_shorthand?(path, options)
path && (options[:to] || options[:action]).nil? && path =~ %r{/[\w/]+$}
path && (options[:to] || options[:action]).nil? && path =~ %r{^/?[-\w]+/[-\w/]+$}
end
def decomposed_match(path, options) # :nodoc:
......
......@@ -1430,6 +1430,15 @@ def test_match_shorthand_inside_nested_namespaces_and_scopes_with_controller
assert_equal 'api/v3/products#list', @response.body
end
def test_not_matching_shorthand_with_dynamic_parameters
draw do
get ':controller/:action/admin'
end
get '/finances/overview/admin'
assert_equal 'finances#overview', @response.body
end
def test_controller_option_with_nesting_and_leading_slash
draw do
scope '/job', controller: 'job' do
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册