Greetings I am migrating old code base to Sanic, but I have encountered an issue related to Sanic routing.
The gist of the problem is that <uuid:uuid>.xml
is considered an invalid URI for Sanic, while for Starlette this worked fine.
Here is my simplified application:
# server.py
from sanic import Request, Sanic, text
app = Sanic("MyApp")
# GOOD: http://localhost:8000/300c4a8c-ddae-4265-b4b3-febf6da2ae31
@app.get("/<uuid:uuid>")
async def good_uri(request, uuid):
return text(str(uuid))
# BAD: http://localhost:8000/300c4a8c-ddae-4265-b4b3-febf6da2ae31.xml
@app.get("/<uuid:uuid>.xml")
async def bad_uri(request, uuid):
"""I want to add .xml extension to an UUID path"""
return text(str(uuid))
The error output I get:
$ sanic server:app
@app.get("/<uuid:uuid>.xml")
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../.venv/lib/python3.12/site-packages/sanic/mixins/routes.py", line 205, in decorator
self._apply_route(route, overwrite=overwrite)
File ".../.venv/lib/python3.12/site-packages/sanic/app.py", line 613, in _apply_route
routes = self.router.add(**params)
^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../.venv/lib/python3.12/site-packages/sanic/router.py", line 149, in add
route = super().add(**params) # type: ignore
^^^^^^^^^^^^^^^^^^^^^
File ".../.venv/lib/python3.12/site-packages/sanic_routing/router.py", line 215, in add
path = parts_to_path(
^^^^^^^^^^^^^^
File ".../.venv/lib/python3.12/site-packages/sanic_routing/utils.py", line 94, in parts_to_path
raise InvalidUsage(f"Invalid declaration: {part}")
sanic_routing.exceptions.InvalidUsage: Invalid declaration: <uuid:uuid>.xml
Could anyone give some pointers, or show me a working workaround that would allow me to have .xml
as a suffix?