Coder Social home page Coder Social logo

Comments (6)

ahopkins avatar ahopkins commented on June 16, 2024

The issue seems to stem from the fact that the group of regex paths that must be evaluated at the end (self._get_non_static_non_path_groups(True)) defaults to router[0] without the method check. It should mirror the method check inside the tree. The patch is to do something like this:

        # Inject regex matching that could not be in the tree
        for group in self._get_non_static_non_path_groups(True):
            route_container = (
                "regex_routes" if group.regex else "dynamic_routes"
            )
            route_idx = 0

            if len(group.routes) > 1:
                route_idx = "route_idx"
                Node._inject_method_check(src, 1, group)

            src.extend(
                [
                    Line(
                        (
                            "match = router.matchers"
                            f"[{group.pattern_idx}].match(path)"
                        ),
                        1,
                    ),
                    Line("if match:", 1),
                    Line("basket['__params__'] = match.groupdict()", 2),
                    Line(
                        (
                            f"return router.{route_container}"
                            f"[{group.segments}][{route_idx}], basket"
                        ),
                        2,
                    ),
                ]
            )

from sanic-routing.

robd003 avatar robd003 commented on June 16, 2024

Just tried the above fixes you suggested and I'm getting the following output when printing router.find_route_src. It's currently raising NoMethod on line 126 in sanic_routing/router.py

def find_route(path, method, router, basket, extra):
    parts = tuple(path[1:].split(router.delimiter))
    num = len(parts)
    if method in frozenset({'POST'}):
        route_idx = 0
    elif method in frozenset({'GET', 'OPTIONS'}):
        route_idx = 1
    else:
        raise NoMethod
    match = router.matchers[0].match(path)
    if match:
        basket['__params__'] = match.groupdict()
        return router.regex_routes[('<__dynamic__:path>',)][0], basket
    raise NotFound
matchers = [
    re.compile(r'^/(?P<foo>[^/]?.*?)$'),
]

from sanic-routing.

robd003 avatar robd003 commented on June 16, 2024

Interestingly it looks like the methods aren't being added correctly and only the POST methods are being applied to the Routes when using the path type.

<Route: path=blorp/<bar:float> methods=frozenset({'POST'})>
<Route: path=blorp/<bar:float> methods=frozenset({'OPTIONS', 'GET'})>
<Route: path=frank/<foo:path> methods=frozenset({'POST'})>
<Route: path=frank/<foo:path> methods=frozenset({'POST'})>

from sanic-routing.

robd003 avatar robd003 commented on June 16, 2024

Another weird thing, if I try to print the tree with path routes it'll basically show up with nothing:

<Node: level=0>

However if I add another type those will be populated

<Node: level=0>
    <Node: part=blorp, level=1>
        <Node: part=__dynamic__:float, level=2, groups=[<RouteGroup: path=blorp/<bar:float> len=2>], dynamic=True>

from sanic-routing.

ahopkins avatar ahopkins commented on June 16, 2024

Okay. Will take a look. Path params are handled differently than all the others.

from sanic-routing.

robd003 avatar robd003 commented on June 16, 2024

@ahopkins Here's my current work in progress to help speed up your work https://github.com/robd003/sanic-routing/tree/fix_issue_43

from sanic-routing.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.