Coverage for tests / test_duplicate_models_openapi.py: 100%
24 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 fastapi import FastAPI 1abcd
2from fastapi.testclient import TestClient 1abcd
3from inline_snapshot import snapshot 1abcd
4from pydantic import BaseModel 1abcd
6app = FastAPI() 1abcd
9class Model(BaseModel): 1abcd
10 pass 1abcd
13class Model2(BaseModel): 1abcd
14 a: Model 1abcd
17class Model3(BaseModel): 1abcd
18 c: Model 1abcd
19 d: Model2 1abcd
22@app.get("/", response_model=Model3) 1abcd
23def f(): 1abcd
24 return {"c": {}, "d": {"a": {}}} 1efg
27client = TestClient(app) 1abcd
30def test_get_api_route(): 1abcd
31 response = client.get("/") 1efg
32 assert response.status_code == 200, response.text 1efg
33 assert response.json() == {"c": {}, "d": {"a": {}}} 1efg
36def test_openapi_schema(): 1abcd
37 response = client.get("/openapi.json") 1hij
38 assert response.status_code == 200, response.text 1hij
39 assert response.json() == snapshot( 1hij
40 {
41 "openapi": "3.1.0",
42 "info": {"title": "FastAPI", "version": "0.1.0"},
43 "paths": {
44 "/": {
45 "get": {
46 "summary": "F",
47 "operationId": "f__get",
48 "responses": {
49 "200": {
50 "description": "Successful Response",
51 "content": {
52 "application/json": {
53 "schema": {
54 "$ref": "#/components/schemas/Model3"
55 }
56 }
57 },
58 }
59 },
60 }
61 }
62 },
63 "components": {
64 "schemas": {
65 "Model": {"title": "Model", "type": "object", "properties": {}},
66 "Model2": {
67 "title": "Model2",
68 "required": ["a"],
69 "type": "object",
70 "properties": {"a": {"$ref": "#/components/schemas/Model"}},
71 },
72 "Model3": {
73 "title": "Model3",
74 "required": ["c", "d"],
75 "type": "object",
76 "properties": {
77 "c": {"$ref": "#/components/schemas/Model"},
78 "d": {"$ref": "#/components/schemas/Model2"},
79 },
80 },
81 }
82 },
83 }
84 )