Coverage for tests / test_custom_schema_fields.py: 100%
22 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1from typing import Annotated 1adbc
3from fastapi import FastAPI 1adbc
4from fastapi.testclient import TestClient 1adbc
5from pydantic import BaseModel, WithJsonSchema 1adbc
7app = FastAPI() 1adbc
10class Item(BaseModel): 1adbc
11 name: str 1abc
13 description: Annotated[str | None, WithJsonSchema({"type": ["string", "null"]})] = ( 1adbc
14 None
15 )
17 model_config = { 1adbc
18 "json_schema_extra": {
19 "x-something-internal": {"level": 4},
20 }
21 }
24@app.get("/foo", response_model=Item) 1adbc
25def foo(): 1adbc
26 return {"name": "Foo item"} 1efgh
29client = TestClient(app) 1adbc
32item_schema = { 1adbc
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(): 1adbc
53 response = client.get("/openapi.json") 1ijkl
54 assert response.status_code == 200, response.text 1ijkl
55 assert response.json()["components"]["schemas"]["Item"] == item_schema 1ijkl
58def test_response(): 1adbc
59 # For coverage
60 response = client.get("/foo") 1efgh
61 assert response.status_code == 200, response.text 1efgh
62 assert response.json() == {"name": "Foo item", "description": None} 1efgh