mirror of https://github.com/encode/starlette.git
Allow path to contain regex meta characters (#932)
* Allow path to contain regex meta characters
This commit is contained in:
parent
e57fa2a6b1
commit
f12e237da5
|
@ -107,7 +107,7 @@ def compile_path(
|
|||
), f"Unknown path convertor '{convertor_type}'"
|
||||
convertor = CONVERTOR_TYPES[convertor_type]
|
||||
|
||||
path_regex += path[idx : match.start()]
|
||||
path_regex += re.escape(path[idx : match.start()])
|
||||
path_regex += f"(?P<{param_name}>{convertor.regex})"
|
||||
|
||||
path_format += path[idx : match.start()]
|
||||
|
@ -117,7 +117,7 @@ def compile_path(
|
|||
|
||||
idx = match.end()
|
||||
|
||||
path_regex += path[idx:] + "$"
|
||||
path_regex += re.escape(path[idx:]) + "$"
|
||||
path_format += path[idx:]
|
||||
|
||||
return re.compile(path_regex), path_format, param_convertors
|
||||
|
|
|
@ -83,6 +83,13 @@ def uuid_converter(request):
|
|||
return JSONResponse({"uuid": str(uuid_param)})
|
||||
|
||||
|
||||
# Route with chars that conflict with regex meta chars
|
||||
@app.route("/path-with-parentheses({param:int})", name="path-with-parentheses")
|
||||
def path_with_parentheses(request):
|
||||
number = request.path_params["param"]
|
||||
return JSONResponse({"int": number})
|
||||
|
||||
|
||||
@app.websocket_route("/ws")
|
||||
async def websocket_endpoint(session):
|
||||
await session.accept()
|
||||
|
@ -146,6 +153,15 @@ def test_route_converters():
|
|||
assert response.json() == {"int": 5}
|
||||
assert app.url_path_for("int-convertor", param=5) == "/int/5"
|
||||
|
||||
# Test path with parentheses
|
||||
response = client.get("/path-with-parentheses(7)")
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {"int": 7}
|
||||
assert (
|
||||
app.url_path_for("path-with-parentheses", param=7)
|
||||
== "/path-with-parentheses(7)"
|
||||
)
|
||||
|
||||
# Test float conversion
|
||||
response = client.get("/float/25.5")
|
||||
assert response.status_code == 200
|
||||
|
|
Loading…
Reference in New Issue