Skip to content

Commit 27f83db

Browse files
fix(sanic): fix handling of non-string arguments (backport #2699) (#2708)
* fix(sanic): fix handling of non-string arguments (#2699) * fix(sanic): fix handling of non-string arguments This change fixes the handling of endpoint paths that take non-string arguments. Fixes #2678. * Update ddtrace/contrib/sanic/patch.py Co-authored-by: Nicolas Vivet <[email protected]> Co-authored-by: Nicolas Vivet <[email protected]> (cherry picked from commit c95322a) * fix: fix latest sanic release note (#2703) Co-authored-by: Gabriele N. Tornetta <[email protected]>
1 parent 868412f commit 27f83db

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

ddtrace/contrib/sanic/patch.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ def _get_path(request):
7878
except sanic.exceptions.SanicException:
7979
return path
8080
for key, value in match_info.items():
81+
try:
82+
value = str(value)
83+
except Exception:
84+
# Best effort
85+
continue
8186
path = path.replace(value, f"<{key}>")
8287
return path
8388

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
Fixed the handling of sanic endpoint paths with non-string arguments.

tests/contrib/sanic/test_sanic.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from sanic import Sanic
77
from sanic.config import DEFAULT_CONFIG
88
from sanic.exceptions import ServerError
9+
from sanic.exceptions import abort
910
from sanic.response import json
1011
from sanic.response import stream
1112
from sanic.response import text
@@ -88,6 +89,14 @@ async def invalid(request):
8889
async def empty(request):
8990
pass
9091

92+
@app.route("/<n:int>/count", methods=["GET"])
93+
async def count(request, n):
94+
try:
95+
pass
96+
except Exception as e:
97+
abort(500, e)
98+
return json({"hello": n})
99+
91100
@app.exception(ServerError)
92101
def handler_exception(request, exception):
93102
return text(exception.args[0], exception.status_code)
@@ -339,3 +348,9 @@ async def test_http_request_header_tracing(tracer, client, test_spans):
339348
assert len(spans[0]) == 2
340349
request_span = spans[0][0]
341350
assert request_span.get_tag("http.request.headers.my-header") == "my_value"
351+
352+
353+
async def test_endpoint_with_numeric_arg(tracer, client, test_spans):
354+
response = await client.get("/42/count")
355+
assert _response_status(response) == 200
356+
assert (await _response_text(response)) == '{"hello":42}'

0 commit comments

Comments
 (0)