Issue Manager / issue-manager (push) Has been cancelled
Build Docs / changes (push) Has been cancelled
Build Docs / langs (push) Has been cancelled
Build Docs / build-docs (push) Has been cancelled
Build Docs / docs-all-green (push) Has been cancelled
Conflict detector / main (push) Has been cancelled
Test Redistribute / test-redistribute (fastapi) (push) Has been cancelled
Test Redistribute / test-redistribute (fastapi-slim) (push) Has been cancelled
Test Redistribute / test-redistribute-alls-green (push) Has been cancelled
Test / lint (push) Has been cancelled
Test / test (pydantic-v1, 3.10) (push) Has been cancelled
Test / test (pydantic-v1, 3.11) (push) Has been cancelled
Test / test (pydantic-v1, 3.13) (push) Has been cancelled
Test / test (pydantic-v1, 3.8) (push) Has been cancelled
Test / test (pydantic-v1, 3.9) (push) Has been cancelled
Test / test (pydantic-v2, 3.10) (push) Has been cancelled
Test / test (pydantic-v2, 3.11) (push) Has been cancelled
Test / test (pydantic-v2, 3.12) (push) Has been cancelled
Test / test (pydantic-v2, 3.13) (push) Has been cancelled
Test / test (pydantic-v2, 3.14) (push) Has been cancelled
Test / test (pydantic-v2, 3.8) (push) Has been cancelled
Test / test (pydantic-v2, 3.9) (push) Has been cancelled
Test / coverage-combine (push) Has been cancelled
Test / check (push) Has been cancelled
Label Approved / label-approved (push) Has been cancelled
FastAPI People Contributors / job (push) Has been cancelled
FastAPI People Sponsors / job (push) Has been cancelled
Update Topic Repos / topic-repos (push) Has been cancelled
FastAPI People / job (push) Has been cancelled
Test / test (pydantic-v1, 3.12) (push) Has been cancelled
76 lines
1.7 KiB
Python
76 lines
1.7 KiB
Python
from typing import Optional
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi._compat import PYDANTIC_V2
|
|
from fastapi.testclient import TestClient
|
|
from pydantic import BaseModel
|
|
from typing_extensions import Annotated
|
|
|
|
if PYDANTIC_V2:
|
|
from pydantic import WithJsonSchema
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
class Item(BaseModel):
|
|
name: str
|
|
|
|
if PYDANTIC_V2:
|
|
description: Annotated[
|
|
Optional[str], WithJsonSchema({"type": ["string", "null"]})
|
|
] = None
|
|
|
|
model_config = {
|
|
"json_schema_extra": {
|
|
"x-something-internal": {"level": 4},
|
|
}
|
|
}
|
|
else:
|
|
description: Optional[str] = None # type: ignore[no-redef]
|
|
|
|
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",
|
|
},
|
|
"description": {
|
|
"title": "Description",
|
|
"type": ["string", "null"] if PYDANTIC_V2 else "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", "description": None}
|