From b2e82685b410d7064ca048130e92183d4cfa4104 Mon Sep 17 00:00:00 2001 From: cakemanny Date: Wed, 27 Mar 2019 02:49:05 +0000 Subject: [PATCH 1/2] 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): From 378a732968d6039304c63baa667f8ea0f7c42e3b Mon Sep 17 00:00:00 2001 From: cakemanny Date: Wed, 27 Mar 2019 22:46:30 +0000 Subject: [PATCH 2/2] fix expected float error message --- tests/test_url_building.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_url_building.py b/tests/test_url_building.py index 5e1c300a..a246aabc 100644 --- a/tests/test_url_building.py +++ b/tests/test_url_building.py @@ -223,7 +223,7 @@ def test_fails_with_number_message(app): expected_error = ( 'Value "foo" for parameter `some_number` ' - "does not match pattern for type `float`: -?[0-9\\\\.]+" + r"does not match pattern for type `float`: -?(?:\d+(?:\.\d*)?|\.\d+)" ) assert str(e.value) == expected_error