]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
:bug: Fix path in path parameters (#272)
authorSebastián Ramírez <tiangolo@gmail.com>
Wed, 29 May 2019 09:34:46 +0000 (13:34 +0400)
committerGitHub <noreply@github.com>
Wed, 29 May 2019 09:34:46 +0000 (13:34 +0400)
docs/src/path_params/tutorial004.py [new file with mode: 0644]
fastapi/routing.py
tests/test_tutorial/test_path_params/__init__.py [new file with mode: 0644]
tests/test_tutorial/test_path_params/test_tutorial004.py [new file with mode: 0644]

diff --git a/docs/src/path_params/tutorial004.py b/docs/src/path_params/tutorial004.py
new file mode 100644 (file)
index 0000000..76adf38
--- /dev/null
@@ -0,0 +1,8 @@
+from fastapi import FastAPI
+
+app = FastAPI()
+
+
+@app.get("/files/{file_path:path}")
+async def read_user_me(file_path: str):
+    return {"file_path": file_path}
index 4e5342c8d68c9c71a2aeb678eef14f95737d12fe..b35a5f45d15e769b4c557e3c42f8233af5b379c0 100644 (file)
@@ -255,10 +255,11 @@ class APIRoute(routing.Route):
         assert inspect.isfunction(endpoint) or inspect.ismethod(
             endpoint
         ), f"An endpoint must be a function or method"
-        self.dependant = get_dependant(path=path, call=self.endpoint)
+        self.dependant = get_dependant(path=self.path_format, call=self.endpoint)
         for depends in self.dependencies[::-1]:
             self.dependant.dependencies.insert(
-                0, get_parameterless_sub_dependant(depends=depends, path=path)
+                0,
+                get_parameterless_sub_dependant(depends=depends, path=self.path_format),
             )
         self.body_field = get_body_field(dependant=self.dependant, name=self.name)
         self.app = request_response(
diff --git a/tests/test_tutorial/test_path_params/__init__.py b/tests/test_tutorial/test_path_params/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/test_tutorial/test_path_params/test_tutorial004.py b/tests/test_tutorial/test_path_params/test_tutorial004.py
new file mode 100644 (file)
index 0000000..e4590db
--- /dev/null
@@ -0,0 +1,91 @@
+from starlette.testclient import TestClient
+
+from path_params.tutorial004 import app
+
+client = TestClient(app)
+
+openapi_schema = {
+    "openapi": "3.0.2",
+    "info": {"title": "Fast API", "version": "0.1.0"},
+    "paths": {
+        "/files/{file_path}": {
+            "get": {
+                "responses": {
+                    "200": {
+                        "description": "Successful Response",
+                        "content": {"application/json": {"schema": {}}},
+                    },
+                    "422": {
+                        "description": "Validation Error",
+                        "content": {
+                            "application/json": {
+                                "schema": {
+                                    "$ref": "#/components/schemas/HTTPValidationError"
+                                }
+                            }
+                        },
+                    },
+                },
+                "summary": "Read User Me",
+                "operationId": "read_user_me_files__file_path__get",
+                "parameters": [
+                    {
+                        "required": True,
+                        "schema": {"title": "File_Path", "type": "string"},
+                        "name": "file_path",
+                        "in": "path",
+                    }
+                ],
+            }
+        }
+    },
+    "components": {
+        "schemas": {
+            "ValidationError": {
+                "title": "ValidationError",
+                "required": ["loc", "msg", "type"],
+                "type": "object",
+                "properties": {
+                    "loc": {
+                        "title": "Location",
+                        "type": "array",
+                        "items": {"type": "string"},
+                    },
+                    "msg": {"title": "Message", "type": "string"},
+                    "type": {"title": "Error Type", "type": "string"},
+                },
+            },
+            "HTTPValidationError": {
+                "title": "HTTPValidationError",
+                "type": "object",
+                "properties": {
+                    "detail": {
+                        "title": "Detail",
+                        "type": "array",
+                        "items": {"$ref": "#/components/schemas/ValidationError"},
+                    }
+                },
+            },
+        }
+    },
+}
+
+
+def test_openapi():
+    response = client.get("/openapi.json")
+    assert response.status_code == 200
+    assert response.json() == openapi_schema
+
+
+def test_file_path():
+    response = client.get("/files/home/johndoe/myfile.txt")
+    print(response.content)
+    assert response.status_code == 200
+    assert response.json() == {"file_path": "home/johndoe/myfile.txt"}
+
+
+def test_root_file_path():
+    response = client.get("/files//home/johndoe/myfile.txt")
+    print(response.content)
+    assert response.status_code == 200
+    assert response.json() == {"file_path": "/home/johndoe/myfile.txt"}