From 91ec7a385c8c9ce54f52ef62706e9d4541fbaf11 Mon Sep 17 00:00:00 2001 From: "Stanislav Lyu." Date: Tue, 22 Oct 2024 21:43:59 +0300 Subject: [PATCH] fix: set correct path_template value for trie node (#3806) (#3807) --- litestar/_asgi/routing_trie/mapping.py | 3 ++- litestar/_asgi/routing_trie/types.py | 6 ++--- .../test_prometheus_exporter_example.py | 24 ++++++++++++++++++- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/litestar/_asgi/routing_trie/mapping.py b/litestar/_asgi/routing_trie/mapping.py index 1a977d478d..c96db46cff 100644 --- a/litestar/_asgi/routing_trie/mapping.py +++ b/litestar/_asgi/routing_trie/mapping.py @@ -111,7 +111,7 @@ def add_route_to_trie( next_node_key = component if next_node_key not in current_node.children: - current_node.children[next_node_key] = create_node(path_template=route.path_format) + current_node.children[next_node_key] = create_node() current_node.child_keys = set(current_node.children.keys()) current_node = current_node.children[next_node_key] @@ -140,6 +140,7 @@ def configure_node( """ from litestar.routes import HTTPRoute, WebSocketRoute + node.path_template = route.path_format if not node.path_parameters: node.path_parameters = {} diff --git a/litestar/_asgi/routing_trie/types.py b/litestar/_asgi/routing_trie/types.py index da86482dfa..a07e3d3567 100644 --- a/litestar/_asgi/routing_trie/types.py +++ b/litestar/_asgi/routing_trie/types.py @@ -34,9 +34,9 @@ class RouteTrieNode: "children", "is_asgi", "is_mount", - "is_static", "is_path_param_node", "is_path_type", + "is_static", "path_parameters", "path_template", ) @@ -68,7 +68,7 @@ class RouteTrieNode: """The path template string used to lower prometheus cardinality when group_path enabled""" -def create_node(path_template: str = "") -> RouteTrieNode: +def create_node() -> RouteTrieNode: """Create a RouteMapNode instance. Returns: @@ -85,5 +85,5 @@ def create_node(path_template: str = "") -> RouteTrieNode: is_static=False, is_path_type=False, path_parameters={}, - path_template=path_template, + path_template="", ) diff --git a/tests/examples/test_contrib/prometheus/test_prometheus_exporter_example.py b/tests/examples/test_contrib/prometheus/test_prometheus_exporter_example.py index 5ea31042c0..569fd614ac 100644 --- a/tests/examples/test_contrib/prometheus/test_prometheus_exporter_example.py +++ b/tests/examples/test_contrib/prometheus/test_prometheus_exporter_example.py @@ -3,7 +3,7 @@ import pytest from prometheus_client import REGISTRY -from litestar import get +from litestar import Controller, Litestar, Request, get from litestar.contrib.prometheus import PrometheusMiddleware from litestar.status_codes import HTTP_200_OK from litestar.testing import TestClient @@ -60,3 +60,25 @@ def home(name: str) -> Dict[str, Any]: assert metrics_exporter_response.status_code == HTTP_200_OK metrics = metrics_exporter_response.content.decode() assert expected_path in metrics + + +def test_correct_population_path_template() -> None: + class TestController(Controller): + path = "/prefix" + + @get("/{id_:int}") + async def b(self, request: Request, id_: int) -> str: + return request.scope["path_template"] + + @get("/{id_:int}/postfix") + async def a(self, request: Request, id_: int) -> str: + return request.scope["path_template"] + + app = Litestar([TestController]) + + with TestClient(app) as client: + without_postfix_resp = client.get("/prefix/1") + with_postfix_resp = client.get("/prefix/1/postfix") + + assert without_postfix_resp.content.decode() == "/prefix/{id_}" + assert with_postfix_resp.content.decode() == "/prefix/{id_}/postfix"