Skip to content

Commit

Permalink
Path Resolver For Route Handler Methods On A Controller (#480)
Browse files Browse the repository at this point in the history
* #455

* removed memory reference

* added temporary test

Co-authored-by: Na'aman Hirschfeld <[email protected]>
  • Loading branch information
infohash and Goldziher authored Sep 15, 2022
1 parent 409296a commit 3156a70
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
6 changes: 5 additions & 1 deletion starlite/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ def __init__(
self.opt: Dict[str, Any] = opt or {}
self.owner: Optional[Union["Controller", "Router"]] = None
self.signature_model: Optional[Type["SignatureModel"]] = None
self.paths = {normalize_path(p) for p in path} if path and isinstance(path, list) else {normalize_path(path or "/")} # type: ignore
self.paths = (
{normalize_path(p) for p in path}
if path and isinstance(path, list)
else {normalize_path(path or "/")} # type: ignore
)

@property
def dependency_name_set(self) -> Set[str]:
Expand Down
9 changes: 5 additions & 4 deletions starlite/router.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import collections
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -208,18 +209,18 @@ def route_handler_method_map(
@property
def route_handler_method_view(
self,
) -> Dict[Union[ASGIRouteHandler, "HttpMethod", HTTPRouteHandler, WebsocketRouteHandler], str]:
) -> Dict[str, List[str]]:
"""
Returns:
A dictionary mapping route handlers to paths
"""
route_map: Dict[Union[ASGIRouteHandler, "HttpMethod", HTTPRouteHandler, WebsocketRouteHandler], str] = {}
route_map: Dict[str, List[str]] = collections.defaultdict(list)
for route in self.routes:
if isinstance(route, HTTPRoute):
for route_handler in route.route_handlers:
route_map[route_handler] = route.path
route_map[route_handler.fn.__qualname__].append(route.path) # type: ignore
else:
route_map[cast("WebSocketRoute", route).route_handler] = route.path
route_map[cast("WebSocketRoute", route).route_handler.fn.__qualname__].append(route.path) # type: ignore
return route_map

@staticmethod
Expand Down
37 changes: 34 additions & 3 deletions tests/test_router_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
HttpMethod,
ImproperlyConfiguredException,
Router,
Starlite,
WebSocket,
get,
patch,
post,
put,
)
from starlite import route as route_decorator
from starlite import websocket
Expand Down Expand Up @@ -125,9 +127,38 @@ def test_register_router_on_itself() -> None:


def test_route_handler_method_view() -> None:
@get(path="/test")
@get(path="/root")
def handler() -> None:
...

router = Router(path="/", route_handlers=[MyController, handler])
assert router.route_handler_method_view[handler] == "/test"
def _handler() -> None:
...

put_handler = put("/modify")(_handler)
post_handler = post("/send")(_handler)

first_router = Router(path="/first", route_handlers=[MyController, handler, post_handler, put_handler])
second_router = Router(path="/second", route_handlers=[MyController, handler, post_handler, put_handler])

# Test routers.
assert first_router.route_handler_method_view[handler.fn.__qualname__] == ["/first/root"] # type: ignore
assert second_router.route_handler_method_view[MyController.get_method.fn.__qualname__] == ["/second/test"] # type: ignore

app = Starlite(route_handlers=[first_router, second_router])
# Test on Starlite instance.
assert app.route_handler_method_view[MyController.ws.fn.__qualname__] == [ # type: ignore
"/first/test/socket",
"/second/test/socket",
]
assert app.route_handler_method_view[put_handler.fn.__qualname__] == [ # type: ignore
"/first/send",
"/first/modify",
"/second/send",
"/second/modify",
]
assert app.route_handler_method_view[put_handler.fn.__qualname__] == [ # type: ignore
"/first/send",
"/first/modify",
"/second/send",
"/second/modify",
]

0 comments on commit 3156a70

Please sign in to comment.