-
-
Notifications
You must be signed in to change notification settings - Fork 390
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: mounted app path interferes with regular paths #3501
Comments
Issue confirmed @0xE111 - I've assigned the issue to you. Thanks! |
@all-contributors add @0xE111 for bug |
I've put up a pull request to add @0xE111! 🎉 |
Thanks @0xE111 for the very detailed report!
This sounds like the correct fix here to get the intended behaviour. A PR would be very welcome :) |
A fix for this issue has been released in v2.9.0 |
Description
According to "Mounting ASGI apps" documentation section, Litestar can mount ASGI apps in sub-paths. So it is expected that if ASGI app is mounted with
path='/magic'
, every route starting with/magic
will be handled by the ASGI app, and any other route will be handled by other handlers. However, it is not true.Imagine this setup:
Here's "expectations VS reality" table:
/magic
Mounted
Mounted
/123/magic/
Parametrized
Mounted
/static/magic/
Static
Static
/non-existent/magic/
Mounted
Why this happens?
litestar/_asgi/routing_trie/traversal.py:parse_path_to_route
method has this line:So instead of matching
/magic
topath
,re.search
is used which searches for occurrence of/magic
anywhere inpath
, thus resulting in "false positives" for strings such as/123/magic/
,/non-existent/magic/
and/non-existent/magic/something
.Possible solution
This cannot be solved by simply using regex:
@asgi("^/magic", is_mount=True)
since
mount_paths_regex
becomesre.compile('/^/magic')
, so it not only doesn't solve the problem, but the/magic
endpoint itself stops working.I believe it may be solved by replacing
mount_paths_regex.search(path)
withmount_paths_regex.match(path)
- I did manual tests and it solved the problem completely, but ofc such a change requires tests to ensure nothing else is broken.I am ready to create a full-fledged pull request with tests once the issue is approved :)
URL to code causing the issue
No response
MCVE
Steps to reproduce
Screenshots
No response
Logs
No response
Litestar Version
2.8.3
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: