Skip to content

Parameter types are broken when using <something:path> #72

Open
@andreymal

Description

from sanic import Sanic
from sanic.response import text

app = Sanic("MyHelloWorldApp")

@app.get("/<id:int>/<subpath:path>")
async def hello_world(request, id, subpath):
    return text(str(type(id)))

Expected response is <class 'int'>, but actual response is <class 'str'>.

Same with <id:float> or <id:uuid>.

When using <id:ymd>, it throws an error:

[2023-06-20 20:15:55 +0300] [3355801] [ERROR] Invalid matching pattern ([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))
Traceback (most recent call last):
  File "lib/python3.8/site-packages/sanic/worker/serve.py", line 117, in worker_serve
    return _serve_http_1(
  File "lib/python3.8/site-packages/sanic/server/runners.py", line 224, in _serve_http_1
    loop.run_until_complete(app._startup())
  File "uvloop/loop.pyx", line 1517, in uvloop.loop.Loop.run_until_complete
  File "lib/python3.8/site-packages/sanic/app.py", line 1580, in _startup
    self.finalize()
  File "lib/python3.8/site-packages/sanic/app.py", line 1551, in finalize
    self.router.finalize()
  File "lib/python3.8/site-packages/sanic/router.py", line 202, in finalize
    super().finalize(*args, **kwargs)
  File "lib/python3.8/site-packages/sanic_routing/router.py", line 329, in finalize
    route.finalize()
  File "lib/python3.8/site-packages/sanic_routing/route.py", line 280, in finalize
    self._compile_regex()
  File "lib/python3.8/site-packages/sanic_routing/route.py", line 267, in _compile_regex
    raise InvalidUsage(f"Invalid matching pattern {pattern}")
sanic_routing.exceptions.InvalidUsage: Invalid matching pattern ([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions