sanic-org / sanic-routing

Internal handler routing for Sanic beginning with v21.3.

Home Page:https://sanicframework.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dynamic node with regex subnode can result in 404

ahopkins opened this issue · comments

In this example, having a subnode with regex needs to make sure the node level return does not include the num>x.

"/<foo>"
r"/<foo>/<invoice:(?P<invoice>[0-9]+)\.pdf>"
def find_route(path, method, router, basket, extra):
    parts = tuple(path[1:].split(router.delimiter))
    num = len(parts)
    if parts[0]:
        if parts[0] == "v2":
            if num > 1:
                basket[1] = parts[1]
                if num > 3:
                    ...
                elif num == 3:
                    basket[2] = parts[2]
                    ...
                if num > 2:
                    raise NotFound
                try:
                    basket['__params__']['foo'] = str(basket[1])
                except (ValueError, KeyError):
                    pass
                else:
                    if method in frozenset({'OPTIONS'}):
                        route_idx = 0
                    elif method in frozenset({'GET'}):
                        route_idx = 1
                    else:
                        raise NoMethod
                    return router.dynamic_routes[('v2', '<foo>')][route_idx], basket
    match = router.matchers[0].match(path)
    if match:
        basket['__params__'] = match.groupdict()
        if method in frozenset({'OPTIONS'}):
            route_idx = 0
        elif method in frozenset({'GET'}):
            route_idx = 1
        else:
            raise NoMethod
        return router.regex_routes[('v2', '<foo>', '<invoice:(?P<invoice>[0-9]+)\\.pdf>')][route_idx], basket
    raise NotFound
matchers = [
    re.compile(r'^/v2/<foo>/(?P<invoice>[0-9]+)\.pdf$'),
]