fastapi
1from fastapi import FastAPI2from fastapi._compat import PYDANTIC_V23from fastapi.testclient import TestClient4from pydantic import BaseModel5
6app = FastAPI()7
8
9class Item(BaseModel):10name: str11
12if PYDANTIC_V2:13model_config = {14"json_schema_extra": {15"x-something-internal": {"level": 4},16}17}18else:19
20class Config:21schema_extra = {22"x-something-internal": {"level": 4},23}24
25
26@app.get("/foo", response_model=Item)27def foo():28return {"name": "Foo item"}29
30
31client = TestClient(app)32
33
34item_schema = {35"title": "Item",36"required": ["name"],37"type": "object",38"x-something-internal": {39"level": 4,40},41"properties": {42"name": {43"title": "Name",44"type": "string",45}46},47}
48
49
50def test_custom_response_schema():51response = client.get("/openapi.json")52assert response.status_code == 200, response.text53assert response.json()["components"]["schemas"]["Item"] == item_schema54
55
56def test_response():57# For coverage58response = client.get("/foo")59assert response.status_code == 200, response.text60assert response.json() == {"name": "Foo item"}61