Coverage for tests/test_openapi_examples.py: 100%
39 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1from typing import Union 1abcde
3from dirty_equals import IsDict 1abcde
4from fastapi import Body, Cookie, FastAPI, Header, Path, Query 1abcde
5from fastapi.testclient import TestClient 1abcde
6from pydantic import BaseModel 1abcde
8app = FastAPI() 1abcde
11class Item(BaseModel): 1abcde
12 data: str 1abcde
15@app.post("/examples/") 1abcde
16def examples( 1abcde
17 item: Item = Body(
18 examples=[
19 {"data": "Data in Body examples, example1"},
20 ],
21 openapi_examples={
22 "Example One": {
23 "summary": "Example One Summary",
24 "description": "Example One Description",
25 "value": {"data": "Data in Body examples, example1"},
26 },
27 "Example Two": {
28 "value": {"data": "Data in Body examples, example2"},
29 },
30 },
31 ),
32):
33 return item 1abcde
36@app.get("/path_examples/{item_id}") 1abcde
37def path_examples( 1abcde
38 item_id: str = Path(
39 examples=[
40 "json_schema_item_1",
41 "json_schema_item_2",
42 ],
43 openapi_examples={
44 "Path One": {
45 "summary": "Path One Summary",
46 "description": "Path One Description",
47 "value": "item_1",
48 },
49 "Path Two": {
50 "value": "item_2",
51 },
52 },
53 ),
54):
55 return item_id 1abcde
58@app.get("/query_examples/") 1abcde
59def query_examples( 1abcde
60 data: Union[str, None] = Query(
61 default=None,
62 examples=[
63 "json_schema_query1",
64 "json_schema_query2",
65 ],
66 openapi_examples={
67 "Query One": {
68 "summary": "Query One Summary",
69 "description": "Query One Description",
70 "value": "query1",
71 },
72 "Query Two": {
73 "value": "query2",
74 },
75 },
76 ),
77):
78 return data 1abcde
81@app.get("/header_examples/") 1abcde
82def header_examples( 1abcde
83 data: Union[str, None] = Header(
84 default=None,
85 examples=[
86 "json_schema_header1",
87 "json_schema_header2",
88 ],
89 openapi_examples={
90 "Header One": {
91 "summary": "Header One Summary",
92 "description": "Header One Description",
93 "value": "header1",
94 },
95 "Header Two": {
96 "value": "header2",
97 },
98 },
99 ),
100):
101 return data 1abcde
104@app.get("/cookie_examples/") 1abcde
105def cookie_examples( 1abcde
106 data: Union[str, None] = Cookie(
107 default=None,
108 examples=["json_schema_cookie1", "json_schema_cookie2"],
109 openapi_examples={
110 "Cookie One": {
111 "summary": "Cookie One Summary",
112 "description": "Cookie One Description",
113 "value": "cookie1",
114 },
115 "Cookie Two": {
116 "value": "cookie2",
117 },
118 },
119 ),
120):
121 return data 1abcde
124client = TestClient(app) 1abcde
127def test_call_api(): 1abcde
128 response = client.post("/examples/", json={"data": "example1"}) 1abcde
129 assert response.status_code == 200, response.text 1abcde
131 response = client.get("/path_examples/foo") 1abcde
132 assert response.status_code == 200, response.text 1abcde
134 response = client.get("/query_examples/") 1abcde
135 assert response.status_code == 200, response.text 1abcde
137 response = client.get("/header_examples/") 1abcde
138 assert response.status_code == 200, response.text 1abcde
140 response = client.get("/cookie_examples/") 1abcde
141 assert response.status_code == 200, response.text 1abcde
144def test_openapi_schema(): 1abcde
145 response = client.get("/openapi.json") 1abcde
146 assert response.status_code == 200, response.text 1abcde
147 assert response.json() == { 1abcde
148 "openapi": "3.1.0",
149 "info": {"title": "FastAPI", "version": "0.1.0"},
150 "paths": {
151 "/examples/": {
152 "post": {
153 "summary": "Examples",
154 "operationId": "examples_examples__post",
155 "requestBody": {
156 "content": {
157 "application/json": {
158 "schema": {
159 "allOf": [{"$ref": "#/components/schemas/Item"}],
160 "title": "Item",
161 "examples": [
162 {"data": "Data in Body examples, example1"}
163 ],
164 },
165 "examples": {
166 "Example One": {
167 "summary": "Example One Summary",
168 "description": "Example One Description",
169 "value": {
170 "data": "Data in Body examples, example1"
171 },
172 },
173 "Example Two": {
174 "value": {
175 "data": "Data in Body examples, example2"
176 }
177 },
178 },
179 }
180 },
181 "required": True,
182 },
183 "responses": {
184 "200": {
185 "description": "Successful Response",
186 "content": {"application/json": {"schema": {}}},
187 },
188 "422": {
189 "description": "Validation Error",
190 "content": {
191 "application/json": {
192 "schema": {
193 "$ref": "#/components/schemas/HTTPValidationError"
194 }
195 }
196 },
197 },
198 },
199 }
200 },
201 "/path_examples/{item_id}": {
202 "get": {
203 "summary": "Path Examples",
204 "operationId": "path_examples_path_examples__item_id__get",
205 "parameters": [
206 {
207 "name": "item_id",
208 "in": "path",
209 "required": True,
210 "schema": {
211 "type": "string",
212 "examples": [
213 "json_schema_item_1",
214 "json_schema_item_2",
215 ],
216 "title": "Item Id",
217 },
218 "examples": {
219 "Path One": {
220 "summary": "Path One Summary",
221 "description": "Path One Description",
222 "value": "item_1",
223 },
224 "Path Two": {"value": "item_2"},
225 },
226 }
227 ],
228 "responses": {
229 "200": {
230 "description": "Successful Response",
231 "content": {"application/json": {"schema": {}}},
232 },
233 "422": {
234 "description": "Validation Error",
235 "content": {
236 "application/json": {
237 "schema": {
238 "$ref": "#/components/schemas/HTTPValidationError"
239 }
240 }
241 },
242 },
243 },
244 }
245 },
246 "/query_examples/": {
247 "get": {
248 "summary": "Query Examples",
249 "operationId": "query_examples_query_examples__get",
250 "parameters": [
251 {
252 "name": "data",
253 "in": "query",
254 "required": False,
255 "schema": IsDict(
256 {
257 "anyOf": [{"type": "string"}, {"type": "null"}],
258 "examples": [
259 "json_schema_query1",
260 "json_schema_query2",
261 ],
262 "title": "Data",
263 }
264 )
265 | IsDict(
266 # TODO: remove when deprecating Pydantic v1
267 {
268 "examples": [
269 "json_schema_query1",
270 "json_schema_query2",
271 ],
272 "type": "string",
273 "title": "Data",
274 }
275 ),
276 "examples": {
277 "Query One": {
278 "summary": "Query One Summary",
279 "description": "Query One Description",
280 "value": "query1",
281 },
282 "Query Two": {"value": "query2"},
283 },
284 }
285 ],
286 "responses": {
287 "200": {
288 "description": "Successful Response",
289 "content": {"application/json": {"schema": {}}},
290 },
291 "422": {
292 "description": "Validation Error",
293 "content": {
294 "application/json": {
295 "schema": {
296 "$ref": "#/components/schemas/HTTPValidationError"
297 }
298 }
299 },
300 },
301 },
302 }
303 },
304 "/header_examples/": {
305 "get": {
306 "summary": "Header Examples",
307 "operationId": "header_examples_header_examples__get",
308 "parameters": [
309 {
310 "name": "data",
311 "in": "header",
312 "required": False,
313 "schema": IsDict(
314 {
315 "anyOf": [{"type": "string"}, {"type": "null"}],
316 "examples": [
317 "json_schema_header1",
318 "json_schema_header2",
319 ],
320 "title": "Data",
321 }
322 )
323 | IsDict(
324 # TODO: remove when deprecating Pydantic v1
325 {
326 "type": "string",
327 "examples": [
328 "json_schema_header1",
329 "json_schema_header2",
330 ],
331 "title": "Data",
332 }
333 ),
334 "examples": {
335 "Header One": {
336 "summary": "Header One Summary",
337 "description": "Header One Description",
338 "value": "header1",
339 },
340 "Header Two": {"value": "header2"},
341 },
342 }
343 ],
344 "responses": {
345 "200": {
346 "description": "Successful Response",
347 "content": {"application/json": {"schema": {}}},
348 },
349 "422": {
350 "description": "Validation Error",
351 "content": {
352 "application/json": {
353 "schema": {
354 "$ref": "#/components/schemas/HTTPValidationError"
355 }
356 }
357 },
358 },
359 },
360 }
361 },
362 "/cookie_examples/": {
363 "get": {
364 "summary": "Cookie Examples",
365 "operationId": "cookie_examples_cookie_examples__get",
366 "parameters": [
367 {
368 "name": "data",
369 "in": "cookie",
370 "required": False,
371 "schema": IsDict(
372 {
373 "anyOf": [{"type": "string"}, {"type": "null"}],
374 "examples": [
375 "json_schema_cookie1",
376 "json_schema_cookie2",
377 ],
378 "title": "Data",
379 }
380 )
381 | IsDict(
382 # TODO: remove when deprecating Pydantic v1
383 {
384 "type": "string",
385 "examples": [
386 "json_schema_cookie1",
387 "json_schema_cookie2",
388 ],
389 "title": "Data",
390 }
391 ),
392 "examples": {
393 "Cookie One": {
394 "summary": "Cookie One Summary",
395 "description": "Cookie One Description",
396 "value": "cookie1",
397 },
398 "Cookie Two": {"value": "cookie2"},
399 },
400 }
401 ],
402 "responses": {
403 "200": {
404 "description": "Successful Response",
405 "content": {"application/json": {"schema": {}}},
406 },
407 "422": {
408 "description": "Validation Error",
409 "content": {
410 "application/json": {
411 "schema": {
412 "$ref": "#/components/schemas/HTTPValidationError"
413 }
414 }
415 },
416 },
417 },
418 }
419 },
420 },
421 "components": {
422 "schemas": {
423 "HTTPValidationError": {
424 "properties": {
425 "detail": {
426 "items": {"$ref": "#/components/schemas/ValidationError"},
427 "type": "array",
428 "title": "Detail",
429 }
430 },
431 "type": "object",
432 "title": "HTTPValidationError",
433 },
434 "Item": {
435 "properties": {"data": {"type": "string", "title": "Data"}},
436 "type": "object",
437 "required": ["data"],
438 "title": "Item",
439 },
440 "ValidationError": {
441 "properties": {
442 "loc": {
443 "items": {
444 "anyOf": [{"type": "string"}, {"type": "integer"}]
445 },
446 "type": "array",
447 "title": "Location",
448 },
449 "msg": {"type": "string", "title": "Message"},
450 "type": {"type": "string", "title": "Error Type"},
451 },
452 "type": "object",
453 "required": ["loc", "msg", "type"],
454 "title": "ValidationError",
455 },
456 }
457 },
458 }