]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
🐛 Support custom OpenAPI / JSON Schema fields in the generated output OpenAPI (#1429)
authorJacob Magnusson <m@jacobian.se>
Sat, 3 Jul 2021 17:15:59 +0000 (19:15 +0200)
committerGitHub <noreply@github.com>
Sat, 3 Jul 2021 17:15:59 +0000 (19:15 +0200)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
fastapi/openapi/models.py
tests/test_custom_schema_fields.py [new file with mode: 0644]

index fd480946dcf1932e41f42e113532e43a82215bf5..efbd88ad74b9cd806fd94f21135088caf7e2c5a4 100644 (file)
@@ -117,6 +117,9 @@ class SchemaBase(BaseModel):
     example: Optional[Any] = None
     deprecated: Optional[bool] = None
 
+    class Config:
+        extra: str = "allow"
+
 
 class Schema(SchemaBase):
     allOf: Optional[List[SchemaBase]] = None
diff --git a/tests/test_custom_schema_fields.py b/tests/test_custom_schema_fields.py
new file mode 100644 (file)
index 0000000..10b0260
--- /dev/null
@@ -0,0 +1,51 @@
+from fastapi import FastAPI
+from fastapi.testclient import TestClient
+from pydantic import BaseModel
+
+app = FastAPI()
+
+
+class Item(BaseModel):
+    name: str
+
+    class Config:
+        schema_extra = {
+            "x-something-internal": {"level": 4},
+        }
+
+
+@app.get("/foo", response_model=Item)
+def foo():
+    return {"name": "Foo item"}
+
+
+client = TestClient(app)
+
+
+item_schema = {
+    "title": "Item",
+    "required": ["name"],
+    "type": "object",
+    "x-something-internal": {
+        "level": 4,
+    },
+    "properties": {
+        "name": {
+            "title": "Name",
+            "type": "string",
+        }
+    },
+}
+
+
+def test_custom_response_schema():
+    response = client.get("/openapi.json")
+    assert response.status_code == 200, response.text
+    assert response.json()["components"]["schemas"]["Item"] == item_schema
+
+
+def test_response():
+    # For coverage
+    response = client.get("/foo")
+    assert response.status_code == 200, response.text
+    assert response.json() == {"name": "Foo item"}