Coverage for tests / test_custom_schema_fields.py: 100%
22 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1from typing import Annotated, Optional 1abcd
3from fastapi import FastAPI 1abcd
4from fastapi.testclient import TestClient 1abcd
5from pydantic import BaseModel, WithJsonSchema 1abcd
7app = FastAPI() 1abcd
10class Item(BaseModel): 1abcd
11 name: str 1abcd
13 description: Annotated[ 1abcd
14 Optional[str], WithJsonSchema({"type": ["string", "null"]})
15 ] = None
17 model_config = { 1abcd
18 "json_schema_extra": {
19 "x-something-internal": {"level": 4},
20 }
21 }
24@app.get("/foo", response_model=Item) 1abcd
25def foo(): 1abcd
26 return {"name": "Foo item"} 1efg
29client = TestClient(app) 1abcd
32item_schema = { 1abcd
33 "title": "Item",
34 "required": ["name"],
35 "type": "object",
36 "x-something-internal": {
37 "level": 4,
38 },
39 "properties": {
40 "name": {
41 "title": "Name",
42 "type": "string",
43 },
44 "description": {
45 "title": "Description",
46 "type": ["string", "null"],
47 },
48 },
49}
52def test_custom_response_schema(): 1abcd
53 response = client.get("/openapi.json") 1hij
54 assert response.status_code == 200, response.text 1hij
55 assert response.json()["components"]["schemas"]["Item"] == item_schema 1hij
58def test_response(): 1abcd
59 # For coverage
60 response = client.get("/foo") 1efg
61 assert response.status_code == 200, response.text 1efg
62 assert response.json() == {"name": "Foo item", "description": None} 1efg