Debian Codegraph @

tests/test_response_model_sub_types.py

raw
1 from fastapi import FastAPI
2 from fastapi.testclient import TestClient
3 from inline_snapshot import snapshot
4 from pydantic import BaseModel
5
6
7 class Model(BaseModel):
8 name: str
9
10
11 app = FastAPI()
12
13
14 @app.get("/valid1", responses={"500": {"model": int}})
15 def valid1():
16 pass
17
18
19 @app.get("/valid2", responses={"500": {"model": list[int]}})
20 def valid2():
21 pass
22
23
24 @app.get("/valid3", responses={"500": {"model": Model}})
25 def valid3():
26 pass
27
28
29 @app.get("/valid4", responses={"500": {"model": list[Model]}})
30 def valid4():
31 pass
32
33
34 client = TestClient(app)
35
36
37 def test_path_operations():
38 response = client.get("/valid1")
39 assert response.status_code == 200, response.text
40 response = client.get("/valid2")
41 assert response.status_code == 200, response.text
42 response = client.get("/valid3")
43 assert response.status_code == 200, response.text
44 response = client.get("/valid4")
45 assert response.status_code == 200, response.text
46
47
48 def test_openapi_schema():
49 response = client.get("/openapi.json")
50 assert response.status_code == 200, response.text
51 assert response.json() == snapshot(
52 {
53 "openapi": "3.1.0",
54 "info": {"title": "FastAPI", "version": "0.1.0"},
55 "paths": {
56 "/valid1": {
57 "get": {
58 "summary": "Valid1",
59 "operationId": "valid1_valid1_get",
60 "responses": {
61 "200": {
62 "description": "Successful Response",
63 "content": {"application/json": {"schema": {}}},
64 },
65 "500": {
66 "description": "Internal Server Error",
67 "content": {
68 "application/json": {
69 "schema": {
70 "title": "Response 500 Valid1 Valid1 Get",
71 "type": "integer",
72 }
73 }
74 },
75 },
76 },
77 }
78 },
79 "/valid2": {
80 "get": {
81 "summary": "Valid2",
82 "operationId": "valid2_valid2_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 "title": "Response 500 Valid2 Valid2 Get",
94 "type": "array",
95 "items": {"type": "integer"},
96 }
97 }
98 },
99 },
100 },
101 }
102 },
103 "/valid3": {
104 "get": {
105 "summary": "Valid3",
106 "operationId": "valid3_valid3_get",
107 "responses": {
108 "200": {
109 "description": "Successful Response",
110 "content": {"application/json": {"schema": {}}},
111 },
112 "500": {
113 "description": "Internal Server Error",
114 "content": {
115 "application/json": {
116 "schema": {"$ref": "#/components/schemas/Model"}
117 }
118 },
119 },
120 },
121 }
122 },
123 "/valid4": {
124 "get": {
125 "summary": "Valid4",
126 "operationId": "valid4_valid4_get",
127 "responses": {
128 "200": {
129 "description": "Successful Response",
130 "content": {"application/json": {"schema": {}}},
131 },
132 "500": {
133 "description": "Internal Server Error",
134 "content": {
135 "application/json": {
136 "schema": {
137 "title": "Response 500 Valid4 Valid4 Get",
138 "type": "array",
139 "items": {
140 "$ref": "#/components/schemas/Model"
141 },
142 }
143 }
144 },
145 },
146 },
147 }
148 },
149 },
150 "components": {
151 "schemas": {
152 "Model": {
153 "title": "Model",
154 "required": ["name"],
155 "type": "object",
156 "properties": {"name": {"title": "Name", "type": "string"}},
157 }
158 }
159 },
160 }
161 )