| 1 |
""" |
| 2 |
Regression test: Ensure app-level responses with Union models and content/examples |
| 3 |
don't accumulate duplicate $ref entries in anyOf arrays. |
| 4 |
See https://github.com/fastapi/fastapi/pull/14463 |
| 5 |
""" |
| 6 |
|
| 7 |
from fastapi import FastAPI |
| 8 |
from fastapi.testclient import TestClient |
| 9 |
from inline_snapshot import snapshot |
| 10 |
from pydantic import BaseModel |
| 11 |
|
| 12 |
|
| 13 |
class ModelA(BaseModel): |
| 14 |
a: str |
| 15 |
|
| 16 |
|
| 17 |
class ModelB(BaseModel): |
| 18 |
b: str |
| 19 |
|
| 20 |
|
| 21 |
app = FastAPI( |
| 22 |
responses={ |
| 23 |
500: { |
| 24 |
"model": ModelA | ModelB, |
| 25 |
"content": {"application/json": {"examples": {"Case A": {"value": "a"}}}}, |
| 26 |
} |
| 27 |
} |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
@app.get("/route1") |
| 32 |
async def route1(): |
| 33 |
pass # pragma: no cover |
| 34 |
|
| 35 |
|
| 36 |
@app.get("/route2") |
| 37 |
async def route2(): |
| 38 |
pass # pragma: no cover |
| 39 |
|
| 40 |
|
| 41 |
client = TestClient(app) |
| 42 |
|
| 43 |
|
| 44 |
def test_openapi_schema(): |
| 45 |
response = client.get("/openapi.json") |
| 46 |
assert response.status_code == 200, response.text |
| 47 |
assert response.json() == snapshot( |
| 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 |
) |