]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
🐛 Fix support for extending `openapi_extras` with parameter lists (#4267)
authorOri Levari <ori.levari@microsoft.com>
Fri, 26 Aug 2022 09:26:20 +0000 (02:26 -0700)
committerGitHub <noreply@github.com>
Fri, 26 Aug 2022 09:26:20 +0000 (09:26 +0000)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
fastapi/utils.py
tests/test_openapi_query_parameter_extension.py [new file with mode: 0644]

index 40750a5b352696860d5ed5238c77b4af0179ecda..0163fd95fc62cc9100970023890eb6a50cdc0793 100644 (file)
@@ -161,6 +161,12 @@ def deep_dict_update(main_dict: Dict[Any, Any], update_dict: Dict[Any, Any]) ->
             and isinstance(value, dict)
         ):
             deep_dict_update(main_dict[key], value)
+        elif (
+            key in main_dict
+            and isinstance(main_dict[key], list)
+            and isinstance(update_dict[key], list)
+        ):
+            main_dict[key] = main_dict[key] + update_dict[key]
         else:
             main_dict[key] = value
 
diff --git a/tests/test_openapi_query_parameter_extension.py b/tests/test_openapi_query_parameter_extension.py
new file mode 100644 (file)
index 0000000..d3996f2
--- /dev/null
@@ -0,0 +1,127 @@
+from typing import Optional
+
+from fastapi import FastAPI
+from fastapi.testclient import TestClient
+
+app = FastAPI()
+
+
+@app.get(
+    "/",
+    openapi_extra={
+        "parameters": [
+            {
+                "required": False,
+                "schema": {"title": "Extra Param 1"},
+                "name": "extra_param_1",
+                "in": "query",
+            },
+            {
+                "required": True,
+                "schema": {"title": "Extra Param 2"},
+                "name": "extra_param_2",
+                "in": "query",
+            },
+        ]
+    },
+)
+def route_with_extra_query_parameters(standard_query_param: Optional[int] = 50):
+    return {}
+
+
+client = TestClient(app)
+
+
+openapi_schema = {
+    "openapi": "3.0.2",
+    "info": {"title": "FastAPI", "version": "0.1.0"},
+    "paths": {
+        "/": {
+            "get": {
+                "summary": "Route With Extra Query Parameters",
+                "operationId": "route_with_extra_query_parameters__get",
+                "parameters": [
+                    {
+                        "required": False,
+                        "schema": {
+                            "title": "Standard Query Param",
+                            "type": "integer",
+                            "default": 50,
+                        },
+                        "name": "standard_query_param",
+                        "in": "query",
+                    },
+                    {
+                        "required": False,
+                        "schema": {"title": "Extra Param 1"},
+                        "name": "extra_param_1",
+                        "in": "query",
+                    },
+                    {
+                        "required": True,
+                        "schema": {"title": "Extra Param 2"},
+                        "name": "extra_param_2",
+                        "in": "query",
+                    },
+                ],
+                "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": {"anyOf": [{"type": "string"}, {"type": "integer"}]},
+                    },
+                    "msg": {"title": "Message", "type": "string"},
+                    "type": {"title": "Error Type", "type": "string"},
+                },
+            },
+        }
+    },
+}
+
+
+def test_openapi():
+    response = client.get("/openapi.json")
+    assert response.status_code == 200, response.text
+    assert response.json() == openapi_schema
+
+
+def test_get_route():
+    response = client.get("/")
+    assert response.status_code == 200, response.text
+    assert response.json() == {}