Debian Codegraph @

tests/test_response_class_no_mediatype.py

raw
1 from fastapi import FastAPI, Response
2 from fastapi.responses import JSONResponse
3 from fastapi.testclient import TestClient
4 from inline_snapshot import snapshot
5 from pydantic import BaseModel
6
7 app = FastAPI()
8
9
10 class JsonApiResponse(JSONResponse):
11 media_type = "application/vnd.api+json"
12
13
14 class Error(BaseModel):
15 status: str
16 title: str
17
18
19 class JsonApiError(BaseModel):
20 errors: list[Error]
21
22
23 @app.get(
24 "/a",
25 response_class=Response,
26 responses={500: {"description": "Error", "model": JsonApiError}},
27 )
28 async def a():
29 pass # pragma: no cover
30
31
32 @app.get("/b", responses={500: {"description": "Error", "model": Error}})
33 async def b():
34 pass # pragma: no cover
35
36
37 client = TestClient(app)
38
39
40 def test_openapi_schema():
41 response = client.get("/openapi.json")
42 assert response.status_code == 200, response.text
43 assert response.json() == snapshot(
44 {
45 "openapi": "3.1.0",
46 "info": {"title": "FastAPI", "version": "0.1.0"},
47 "paths": {
48 "/a": {
49 "get": {
50 "responses": {
51 "500": {
52 "description": "Error",
53 "content": {
54 "application/json": {
55 "schema": {
56 "$ref": "#/components/schemas/JsonApiError"
57 }
58 }
59 },
60 },
61 "200": {"description": "Successful Response"},
62 },
63 "summary": "A",
64 "operationId": "a_a_get",
65 }
66 },
67 "/b": {
68 "get": {
69 "responses": {
70 "500": {
71 "description": "Error",
72 "content": {
73 "application/json": {
74 "schema": {"$ref": "#/components/schemas/Error"}
75 }
76 },
77 },
78 "200": {
79 "description": "Successful Response",
80 "content": {"application/json": {"schema": {}}},
81 },
82 },
83 "summary": "B",
84 "operationId": "b_b_get",
85 }
86 },
87 },
88 "components": {
89 "schemas": {
90 "Error": {
91 "title": "Error",
92 "required": ["status", "title"],
93 "type": "object",
94 "properties": {
95 "status": {"title": "Status", "type": "string"},
96 "title": {"title": "Title", "type": "string"},
97 },
98 },
99 "JsonApiError": {
100 "title": "JsonApiError",
101 "required": ["errors"],
102 "type": "object",
103 "properties": {
104 "errors": {
105 "title": "Errors",
106 "type": "array",
107 "items": {"$ref": "#/components/schemas/Error"},
108 }
109 },
110 },
111 }
112 },
113 }
114 )