]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
:bug: Fix OpenAPI URL format for Starlette convertors (#234)
authoreuri10 <euri10@users.noreply.github.com>
Thu, 16 May 2019 13:55:14 +0000 (15:55 +0200)
committerSebastián Ramírez <tiangolo@gmail.com>
Thu, 16 May 2019 13:55:14 +0000 (17:55 +0400)
fastapi/openapi/utils.py
tests/test_starlette_urlconvertors.py [new file with mode: 0644]

index 760f879b8364d7f6f94b50b510d23d2fad878950..87e223cb6adb3995df528f0fd6ba90e7b55c222f 100644 (file)
@@ -114,7 +114,7 @@ def get_openapi_operation_request_body(
 def generate_operation_id(*, route: routing.APIRoute, method: str) -> str:
     if route.operation_id:
         return route.operation_id
-    path: str = route.path
+    path: str = route.path_format
     operation_id = route.name + path
     operation_id = operation_id.replace("{", "_").replace("}", "_").replace("/", "_")
     operation_id = operation_id + "_" + method.lower()
@@ -253,7 +253,9 @@ def get_openapi(
             if result:
                 path, security_schemes, path_definitions = result
                 if path:
-                    paths.setdefault(openapi_prefix + route.path, {}).update(path)
+                    paths.setdefault(openapi_prefix + route.path_format, {}).update(
+                        path
+                    )
                 if security_schemes:
                     components.setdefault("securitySchemes", {}).update(
                         security_schemes
diff --git a/tests/test_starlette_urlconvertors.py b/tests/test_starlette_urlconvertors.py
new file mode 100644 (file)
index 0000000..c928d55
--- /dev/null
@@ -0,0 +1,51 @@
+from fastapi import FastAPI, Path
+from starlette.testclient import TestClient
+
+app = FastAPI()
+
+
+@app.get("/int/{param:int}")
+def int_convertor(param: int = Path(...)):
+    return {"int": param}
+
+
+@app.get("/float/{param:float}")
+def float_convertor(param: float = Path(...)):
+    return {"float": param}
+
+
+@app.get("/path/{param:path}")
+def path_convertor(param: str = Path(...)):
+    return {"path": param}
+
+
+client = TestClient(app)
+
+
+def test_route_converters_int():
+    # Test integer conversion
+    response = client.get("/int/5")
+    assert response.status_code == 200
+    assert response.json() == {"int": 5}
+    assert app.url_path_for("int_convertor", param=5) == "/int/5"
+
+
+def test_route_converters_float():
+    # Test float conversion
+    response = client.get("/float/25.5")
+    assert response.status_code == 200
+    assert response.json() == {"float": 25.5}
+    assert app.url_path_for("float_convertor", param=25.5) == "/float/25.5"
+
+
+def test_route_converters_path():
+    # Test path conversion
+    response = client.get("/path/some/example")
+    assert response.status_code == 200
+    assert response.json() == {"path": "some/example"}
+
+
+def test_url_path_for_path_convertor():
+    assert (
+        app.url_path_for("path_convertor", param="some/example") == "/path/some/example"
+    )