Coverage for tests / test_schema_ref_pydantic_v2.py: 100%
23 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 Any 1abdc
3import pytest 1abdc
4from fastapi import FastAPI 1abdc
5from fastapi.testclient import TestClient 1abdc
6from inline_snapshot import snapshot 1abdc
7from pydantic import BaseModel, ConfigDict, Field 1abdc
10@pytest.fixture(name="client") 1abdc
11def get_client(): 1abdc
12 app = FastAPI() 1abc
14 class ModelWithRef(BaseModel): 1abc
15 ref: str = Field(validation_alias="$ref", serialization_alias="$ref") 1abc
16 model_config = ConfigDict(validate_by_alias=True, serialize_by_alias=True) 1abc
18 @app.get("/", response_model=ModelWithRef) 1abc
19 async def read_root() -> Any: 1abc
20 return {"$ref": "some-ref"} 1efg
22 client = TestClient(app) 1abc
23 return client 1abc
26def test_get(client: TestClient): 1abdc
27 response = client.get("/") 1efg
28 assert response.json() == {"$ref": "some-ref"} 1efg
31def test_openapi_schema(client: TestClient): 1abdc
32 response = client.get("openapi.json") 1hij
33 assert response.json() == snapshot( 1hij
34 {
35 "openapi": "3.1.0",
36 "info": {"title": "FastAPI", "version": "0.1.0"},
37 "paths": {
38 "/": {
39 "get": {
40 "summary": "Read Root",
41 "operationId": "read_root__get",
42 "responses": {
43 "200": {
44 "description": "Successful Response",
45 "content": {
46 "application/json": {
47 "schema": {
48 "$ref": "#/components/schemas/ModelWithRef"
49 }
50 }
51 },
52 }
53 },
54 }
55 }
56 },
57 "components": {
58 "schemas": {
59 "ModelWithRef": {
60 "properties": {"$ref": {"type": "string", "title": "$Ref"}},
61 "type": "object",
62 "required": ["$ref"],
63 "title": "ModelWithRef",
64 }
65 }
66 },
67 }
68 )