-
-
Notifications
You must be signed in to change notification settings - Fork 364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: Routing based on path parameter data type is not using first matching route handler #3622
Comments
I faced the same problem. I'm trying to resolve |
I managed to reproduce this issue with the following unit test. from litestar import get
from litestar.testing.helpers import create_test_client
def test_conflicting_handlers() -> None:
@get("/{id:int}")
async def hello_world_int(id: int) -> str:
return f"Hello, world (int)! {id}"
@get("/{id:str}")
async def hello_world_str(id: str) -> str:
return f"Hello, world (str)! {id}"
with create_test_client(route_handlers=[hello_world_int, hello_world_str]) as client:
response = client.get("/42")
assert response.status_code == 200
assert "int" in response.text # FAILED
response = client.get("/ABC")
assert response.status_code == 200
assert "str" in response.text |
Hi @provinzkraut @Goldziher I need your guidance to come up with a solution. Please guide me towards someone else if you are not the right people to speak to. Also please guide me to the forum where we should have this discussion. The problem here is that the # this is the line that checks if a PathParameterSentinel is already in the children of the parent.
# on the second route it is already there and a new node is not created
if next_node_key not in current_node.
current_node.children[next_node_key] = create_node(path_template=route.path_format) Now that I understand the problem there are 2 solutions:
My vote is against introducing this new feature, and instead work on proper validations. @get("/{id}")
async def hello_world(id: int | str) -> str:
return f"Hello, world! {id}"
app = Litestar([hello_world]) |
Thanks for the investigation here @IvanovCosmin!
I agree. That seems to be the proper way to handle this. |
Description
Was told Litestar is intended to route differently based on the type of the path parameter:
However, when providing route handlers in the order of
int path param handler
followed bystr path param handler
, thestr path param handler
is the only one that is called in the example code below.URL to code causing the issue
No response
MCVE
Steps to reproduce
Screenshots
No response
Logs
No response
Litestar Version
2.9.1
Platform
Note
While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.
Check out all issues funded or available for funding on our Polar.sh dashboard
The text was updated successfully, but these errors were encountered: