[FIX] http_routing: prevent unslug to fail when there is anchor or qs
Due to the end part of the regex `(?=$|/)`, it will not find and thus not unslug string if they end up with a query string or and anchor except if there is a trailing slash before. First, it's unlikely that there will be a trailing slash as it's not common and on top of that, Odoo try to enforce non trailing slash in URL. Second, it's not that hard to makes those cases work. Before this commit, the following string would "match" and be unslug: - /blog-1 - /blog-1/ - /blog-1/register - /blog-1/?qs=2 - /blog-1/#anchor But those would not: - /blog-1?qs=2 - /blog-1#anchor Part-of: odoo/odoo#107782
This commit is contained in:
@@ -100,7 +100,7 @@ def slug(value):
|
||||
|
||||
|
||||
# NOTE: as the pattern is used as it for the ModelConverter (ir_http.py), do not use any flags
|
||||
_UNSLUG_RE = re.compile(r'(?:(\w{1,2}|\w[A-Za-z0-9-_]+?\w)-)?(-?\d+)(?=$|/)')
|
||||
_UNSLUG_RE = re.compile(r'(?:(\w{1,2}|\w[A-Za-z0-9-_]+?\w)-)?(-?\d+)(?=$|\/|#|\?)')
|
||||
|
||||
|
||||
def unslug(s):
|
||||
|
||||
@@ -21,10 +21,16 @@ class TestUnslug(BaseCase):
|
||||
'--1': (None, None),
|
||||
'foo---1': (None, None),
|
||||
'foo1': (None, None),
|
||||
# qs & anchor & trailing slash
|
||||
'foo-1/': ('foo', 1),
|
||||
'foo-1/?qs=1': ('foo', 1),
|
||||
'foo-1/#anchor': ('foo', 1),
|
||||
'foo-1?qs=1': ('foo', 1),
|
||||
'foo-1#anchor': ('foo', 1),
|
||||
}
|
||||
|
||||
for slug, expected in tests.items():
|
||||
self.assertEqual(unslug(slug), expected)
|
||||
self.assertEqual(unslug(slug), expected, "%r case failed" % slug)
|
||||
|
||||
class TestTitleToSlug(BaseCase):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user