Coverage for tests / test_additional_responses_union_duplicate_anyof.py: 100%
18 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
1"""
2Regression test: Ensure app-level responses with Union models and content/examples
3don't accumulate duplicate $ref entries in anyOf arrays.
4See https://github.com/fastapi/fastapi/pull/14463
5"""
7from fastapi import FastAPI 1adbc
8from fastapi.testclient import TestClient 1adbc
9from inline_snapshot import snapshot 1adbc
10from pydantic import BaseModel 1adbc
13class ModelA(BaseModel): 1adbc
14 a: str 1abc
17class ModelB(BaseModel): 1adbc
18 b: str 1abc
21app = FastAPI( 1adbc
22 responses={
23 500: {
24 "model": ModelA | ModelB,
25 "content": {"application/json": {"examples": {"Case A": {"value": "a"}}}},
26 }
27 }
28)
31@app.get("/route1") 1adbc
32async def route1(): 1adbc
33 pass # pragma: no cover
36@app.get("/route2") 1adbc
37async def route2(): 1adbc
38 pass # pragma: no cover
41client = TestClient(app) 1adbc
44def test_openapi_schema(): 1adbc
45 response = client.get("/openapi.json") 1efgh
46 assert response.status_code == 200, response.text 1efgh
47 assert response.json() == snapshot( 1efgh
48 {
49 "openapi": "3.1.0",
50 "info": {"title": "FastAPI", "version": "0.1.0"},
51 "paths": {
52 "/route1": {
53 "get": {
54 "summary": "Route1",
55 "operationId": "route1_route1_get",
56 "responses": {
57 "200": {
58 "description": "Successful Response",
59 "content": {"application/json": {"schema": {}}},
60 },
61 "500": {
62 "description": "Internal Server Error",
63 "content": {
64 "application/json": {
65 "schema": {
66 "anyOf": [
67 {"$ref": "#/components/schemas/ModelA"},
68 {"$ref": "#/components/schemas/ModelB"},
69 ],
70 "title": "Response 500 Route1 Route1 Get",
71 },
72 "examples": {"Case A": {"value": "a"}},
73 }
74 },
75 },
76 },
77 }
78 },
79 "/route2": {
80 "get": {
81 "summary": "Route2",
82 "operationId": "route2_route2_get",
83 "responses": {
84 "200": {
85 "description": "Successful Response",
86 "content": {"application/json": {"schema": {}}},
87 },
88 "500": {
89 "description": "Internal Server Error",
90 "content": {
91 "application/json": {
92 "schema": {
93 "anyOf": [
94 {"$ref": "#/components/schemas/ModelA"},
95 {"$ref": "#/components/schemas/ModelB"},
96 ],
97 "title": "Response 500 Route2 Route2 Get",
98 },
99 "examples": {"Case A": {"value": "a"}},
100 }
101 },
102 },
103 },
104 }
105 },
106 },
107 "components": {
108 "schemas": {
109 "ModelA": {
110 "properties": {"a": {"type": "string", "title": "A"}},
111 "type": "object",
112 "required": ["a"],
113 "title": "ModelA",
114 },
115 "ModelB": {
116 "properties": {"b": {"type": "string", "title": "B"}},
117 "type": "object",
118 "required": ["b"],
119 "title": "ModelB",
120 },
121 }
122 },
123 }
124 )