Svoboda | Graniru | BBC Russia | Golosameriki | Facebook
Skip to content
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

Open
1 of 4 tasks
fernhaven opened this issue Jul 11, 2024 · 1 comment
Open
1 of 4 tasks
Labels
Bug 🐛 This is something that is not working as expected

Comments

@fernhaven
Copy link

fernhaven commented Jul 11, 2024

Description

Was told Litestar is intended to route differently based on the type of the path parameter:

Q: Is Litestar intended to route differently based on the type of the path parameter
A: ... this only works if the parameters you pass in are actually compatible and distinct, e.g. using a str is basically a catch-all, so if you provide that last, it will always match

However, when providing route handlers in the order of int path param handler followed by str path param handler, the str path param handler is the only one that is called in the example code below.

URL to code causing the issue

No response

MCVE

from litestar import Litestar, get


@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}"

# Expect that path "/42" would route to hello_world_int
# and path "/abc" would go to hello_world_str
app = Litestar([hello_world_int, hello_world_str])

Steps to reproduce

1. Run app: `litestar run`
2. In browser access: 127.0.0.1:8000/42
3. Result: Hello, world (str)! 42  
  Error: (expected to see "(int)" rather than "(str)"
4. In browser access: 127.0.0.1:8000/ABC
5. Result (as expected): Hello, world (str)! ABC

Screenshots

No response

Logs

No response

Litestar Version

2.9.1

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

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

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.
Fund with Polar
@fernhaven fernhaven added the Bug 🐛 This is something that is not working as expected label Jul 11, 2024
@chyvak357
Copy link

I faced the same problem. I'm trying to resolve uuid and int as path parameter, but in all cases only the first declared function will be called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🐛 This is something that is not working as expected
Projects
None yet
Development

No branches or pull requests

2 participants