From 0ed6c9234186ad61b6e6f21b557c978aed799253 Mon Sep 17 00:00:00 2001 From: Jacob Magnusson Date: Sat, 3 Jul 2021 19:15:59 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Support=20custom=20OpenAPI=20/?= =?UTF-8?q?=20JSON=20Schema=20fields=20in=20the=20generated=20output=20Ope?= =?UTF-8?q?nAPI=20(#1429)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sebastián Ramírez --- fastapi/openapi/models.py | 3 ++ tests/test_custom_schema_fields.py | 51 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 tests/test_custom_schema_fields.py diff --git a/fastapi/openapi/models.py b/fastapi/openapi/models.py index fd480946d..efbd88ad7 100644 --- a/fastapi/openapi/models.py +++ b/fastapi/openapi/models.py @@ -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 index 000000000..10b02608c --- /dev/null +++ b/tests/test_custom_schema_fields.py @@ -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"}