diff --git a/sanic/router.py b/sanic/router.py index dcf3aeb5..4c1ea0a0 100644 --- a/sanic/router.py +++ b/sanic/router.py @@ -18,7 +18,7 @@ Parameter = namedtuple("Parameter", ["name", "cast"]) REGEX_TYPES = { "string": (str, r"[^/]+"), "int": (int, r"-?\d+"), - "number": (float, r"-?[0-9\\.]+"), + "number": (float, r"-?(?:\d+(?:\.\d*)?|\.\d+)"), "alpha": (str, r"[A-Za-z]+"), "path": (str, r"[^/].*?"), "uuid": ( diff --git a/tests/test_routes.py b/tests/test_routes.py index f3152935..3ccef135 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -365,9 +365,18 @@ def test_dynamic_route_number(app): request, response = app.test_client.get("/weight/1234.56") assert response.status == 200 + request, response = app.test_client.get("/weight/.12") + assert response.status == 200 + + request, response = app.test_client.get("/weight/12.") + assert response.status == 200 + request, response = app.test_client.get("/weight/1234-56") assert response.status == 404 + request, response = app.test_client.get("/weight/12.34.56") + assert response.status == 404 + def test_dynamic_route_regex(app): @app.route("/folder/") @@ -672,9 +681,18 @@ def test_dynamic_add_route_number(app): request, response = app.test_client.get("/weight/1234.56") assert response.status == 200 + request, response = app.test_client.get("/weight/.12") + assert response.status == 200 + + request, response = app.test_client.get("/weight/12.") + assert response.status == 200 + request, response = app.test_client.get("/weight/1234-56") assert response.status == 404 + request, response = app.test_client.get("/weight/12.34.56") + assert response.status == 404 + def test_dynamic_add_route_regex(app): async def handler(request, folder_id):