]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
:bug: Fix declaring a single parameter per name (#994)
authormerowinger92 <merowinger92@users.noreply.github.com>
Fri, 28 Feb 2020 21:36:30 +0000 (22:36 +0100)
committerGitHub <noreply@github.com>
Fri, 28 Feb 2020 21:36:30 +0000 (22:36 +0100)
fastapi/openapi/utils.py
tests/test_param_in_path_and_dependency.py [new file with mode: 0644]

index d53ee6b97b736bf8bd0ced4b6aa741859ca62779..91f90ec583785ac0089aa547ca60d75303f96e93 100644 (file)
@@ -180,7 +180,9 @@ def get_openapi_path(
             operation_parameters = get_openapi_operation_parameters(all_route_params)
             parameters.extend(operation_parameters)
             if parameters:
-                operation["parameters"] = parameters
+                operation["parameters"] = list(
+                    {param["name"]: param for param in parameters}.values()
+                )
             if method in METHODS_WITH_BODY:
                 request_body_oai = get_openapi_operation_request_body(
                     body_field=route.body_field, model_name_map=model_name_map
diff --git a/tests/test_param_in_path_and_dependency.py b/tests/test_param_in_path_and_dependency.py
new file mode 100644 (file)
index 0000000..55b667e
--- /dev/null
@@ -0,0 +1,93 @@
+from fastapi import Depends, FastAPI
+from starlette.testclient import TestClient
+
+app = FastAPI()
+
+
+async def user_exists(user_id: int):
+    return True
+
+
+@app.get("/users/{user_id}", dependencies=[Depends(user_exists)])
+async def read_users(user_id: int):
+    pass
+
+
+client = TestClient(app)
+
+openapi_schema = {
+    "openapi": "3.0.2",
+    "info": {"title": "FastAPI", "version": "0.1.0"},
+    "paths": {
+        "/users/{user_id}": {
+            "get": {
+                "summary": "Read Users",
+                "operationId": "read_users_users__user_id__get",
+                "parameters": [
+                    {
+                        "required": True,
+                        "schema": {"title": "User Id", "type": "integer"},
+                        "name": "user_id",
+                        "in": "path",
+                    },
+                ],
+                "responses": {
+                    "200": {
+                        "description": "Successful Response",
+                        "content": {"application/json": {"schema": {}}},
+                    },
+                    "422": {
+                        "description": "Validation Error",
+                        "content": {
+                            "application/json": {
+                                "schema": {
+                                    "$ref": "#/components/schemas/HTTPValidationError"
+                                }
+                            }
+                        },
+                    },
+                },
+            }
+        }
+    },
+    "components": {
+        "schemas": {
+            "HTTPValidationError": {
+                "title": "HTTPValidationError",
+                "type": "object",
+                "properties": {
+                    "detail": {
+                        "title": "Detail",
+                        "type": "array",
+                        "items": {"$ref": "#/components/schemas/ValidationError"},
+                    }
+                },
+            },
+            "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"},
+                },
+            },
+        }
+    },
+}
+
+
+def test_reused_param():
+    response = client.get("/openapi.json")
+    data = response.json()
+    assert data == openapi_schema
+
+
+def test_read_users():
+    response = client.get("/users/42")
+    assert response.status_code == 200