From b2e82685b410d7064ca048130e92183d4cfa4104 Mon Sep 17 00:00:00 2001 From: cakemanny Date: Wed, 27 Mar 2019 02:49:05 +0000 Subject: [PATCH] stop number route accepting excess '.'s We stop getting: ValueError: could not convert string to float: '12.34.56' when passing 12.34.56 as a number route parameter argument. By accepting ".12" and "12.", this is a non-breaking change. All valid floats described by [0-9\.]+ are still accepted, just invalid ones are now rejected. --- sanic/router.py | 2 +- tests/test_routes.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) 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):