Coverage for tests/test_openapi_examples.py: 100%
39 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-01-13 13:38 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-01-13 13:38 +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 1fghij
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 1fghij
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 1fghij
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 1fghij
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 1fghij
124client = TestClient(app) 1abcde
127def test_call_api(): 1abcde
128 response = client.post("/examples/", json={"data": "example1"}) 1fghij
129 assert response.status_code == 200, response.text 1fghij
131 response = client.get("/path_examples/foo") 1fghij
132 assert response.status_code == 200, response.text 1fghij
134 response = client.get("/query_examples/") 1fghij
135 assert response.status_code == 200, response.text 1fghij
137 response = client.get("/header_examples/") 1fghij
138 assert response.status_code == 200, response.text 1fghij
140 response = client.get("/cookie_examples/") 1fghij
141 assert response.status_code == 200, response.text 1fghij
144def test_openapi_schema(): 1abcde
145 response = client.get("/openapi.json") 1klmno
146 assert response.status_code == 200, response.text 1klmno
147 assert response.json() == { 1klmno
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": IsDict(
159 {
160 "$ref": "#/components/schemas/Item",
161 "examples": [
162 {"data": "Data in Body examples, example1"}
163 ],
164 }
165 )
166 | IsDict(
167 {
168 # TODO: remove when deprecating Pydantic v1
169 "allOf": [
170 {"$ref": "#/components/schemas/Item"}
171 ],
172 "title": "Item",
173 "examples": [
174 {"data": "Data in Body examples, example1"}
175 ],
176 }
177 ),
178 "examples": {
179 "Example One": {
180 "summary": "Example One Summary",
181 "description": "Example One Description",
182 "value": {
183 "data": "Data in Body examples, example1"
184 },
185 },
186 "Example Two": {
187 "value": {
188 "data": "Data in Body examples, example2"
189 }
190 },
191 },
192 }
193 },
194 "required": True,
195 },
196 "responses": {
197 "200": {
198 "description": "Successful Response",
199 "content": {"application/json": {"schema": {}}},
200 },
201 "422": {
202 "description": "Validation Error",
203 "content": {
204 "application/json": {
205 "schema": {
206 "$ref": "#/components/schemas/HTTPValidationError"
207 }
208 }
209 },
210 },
211 },
212 }
213 },
214 "/path_examples/{item_id}": {
215 "get": {
216 "summary": "Path Examples",
217 "operationId": "path_examples_path_examples__item_id__get",
218 "parameters": [
219 {
220 "name": "item_id",
221 "in": "path",
222 "required": True,
223 "schema": {
224 "type": "string",
225 "examples": [
226 "json_schema_item_1",
227 "json_schema_item_2",
228 ],
229 "title": "Item Id",
230 },
231 "examples": {
232 "Path One": {
233 "summary": "Path One Summary",
234 "description": "Path One Description",
235 "value": "item_1",
236 },
237 "Path Two": {"value": "item_2"},
238 },
239 }
240 ],
241 "responses": {
242 "200": {
243 "description": "Successful Response",
244 "content": {"application/json": {"schema": {}}},
245 },
246 "422": {
247 "description": "Validation Error",
248 "content": {
249 "application/json": {
250 "schema": {
251 "$ref": "#/components/schemas/HTTPValidationError"
252 }
253 }
254 },
255 },
256 },
257 }
258 },
259 "/query_examples/": {
260 "get": {
261 "summary": "Query Examples",
262 "operationId": "query_examples_query_examples__get",
263 "parameters": [
264 {
265 "name": "data",
266 "in": "query",
267 "required": False,
268 "schema": IsDict(
269 {
270 "anyOf": [{"type": "string"}, {"type": "null"}],
271 "examples": [
272 "json_schema_query1",
273 "json_schema_query2",
274 ],
275 "title": "Data",
276 }
277 )
278 | IsDict(
279 # TODO: remove when deprecating Pydantic v1
280 {
281 "examples": [
282 "json_schema_query1",
283 "json_schema_query2",
284 ],
285 "type": "string",
286 "title": "Data",
287 }
288 ),
289 "examples": {
290 "Query One": {
291 "summary": "Query One Summary",
292 "description": "Query One Description",
293 "value": "query1",
294 },
295 "Query Two": {"value": "query2"},
296 },
297 }
298 ],
299 "responses": {
300 "200": {
301 "description": "Successful Response",
302 "content": {"application/json": {"schema": {}}},
303 },
304 "422": {
305 "description": "Validation Error",
306 "content": {
307 "application/json": {
308 "schema": {
309 "$ref": "#/components/schemas/HTTPValidationError"
310 }
311 }
312 },
313 },
314 },
315 }
316 },
317 "/header_examples/": {
318 "get": {
319 "summary": "Header Examples",
320 "operationId": "header_examples_header_examples__get",
321 "parameters": [
322 {
323 "name": "data",
324 "in": "header",
325 "required": False,
326 "schema": IsDict(
327 {
328 "anyOf": [{"type": "string"}, {"type": "null"}],
329 "examples": [
330 "json_schema_header1",
331 "json_schema_header2",
332 ],
333 "title": "Data",
334 }
335 )
336 | IsDict(
337 # TODO: remove when deprecating Pydantic v1
338 {
339 "type": "string",
340 "examples": [
341 "json_schema_header1",
342 "json_schema_header2",
343 ],
344 "title": "Data",
345 }
346 ),
347 "examples": {
348 "Header One": {
349 "summary": "Header One Summary",
350 "description": "Header One Description",
351 "value": "header1",
352 },
353 "Header Two": {"value": "header2"},
354 },
355 }
356 ],
357 "responses": {
358 "200": {
359 "description": "Successful Response",
360 "content": {"application/json": {"schema": {}}},
361 },
362 "422": {
363 "description": "Validation Error",
364 "content": {
365 "application/json": {
366 "schema": {
367 "$ref": "#/components/schemas/HTTPValidationError"
368 }
369 }
370 },
371 },
372 },
373 }
374 },
375 "/cookie_examples/": {
376 "get": {
377 "summary": "Cookie Examples",
378 "operationId": "cookie_examples_cookie_examples__get",
379 "parameters": [
380 {
381 "name": "data",
382 "in": "cookie",
383 "required": False,
384 "schema": IsDict(
385 {
386 "anyOf": [{"type": "string"}, {"type": "null"}],
387 "examples": [
388 "json_schema_cookie1",
389 "json_schema_cookie2",
390 ],
391 "title": "Data",
392 }
393 )
394 | IsDict(
395 # TODO: remove when deprecating Pydantic v1
396 {
397 "type": "string",
398 "examples": [
399 "json_schema_cookie1",
400 "json_schema_cookie2",
401 ],
402 "title": "Data",
403 }
404 ),
405 "examples": {
406 "Cookie One": {
407 "summary": "Cookie One Summary",
408 "description": "Cookie One Description",
409 "value": "cookie1",
410 },
411 "Cookie Two": {"value": "cookie2"},
412 },
413 }
414 ],
415 "responses": {
416 "200": {
417 "description": "Successful Response",
418 "content": {"application/json": {"schema": {}}},
419 },
420 "422": {
421 "description": "Validation Error",
422 "content": {
423 "application/json": {
424 "schema": {
425 "$ref": "#/components/schemas/HTTPValidationError"
426 }
427 }
428 },
429 },
430 },
431 }
432 },
433 },
434 "components": {
435 "schemas": {
436 "HTTPValidationError": {
437 "properties": {
438 "detail": {
439 "items": {"$ref": "#/components/schemas/ValidationError"},
440 "type": "array",
441 "title": "Detail",
442 }
443 },
444 "type": "object",
445 "title": "HTTPValidationError",
446 },
447 "Item": {
448 "properties": {"data": {"type": "string", "title": "Data"}},
449 "type": "object",
450 "required": ["data"],
451 "title": "Item",
452 },
453 "ValidationError": {
454 "properties": {
455 "loc": {
456 "items": {
457 "anyOf": [{"type": "string"}, {"type": "integer"}]
458 },
459 "type": "array",
460 "title": "Location",
461 },
462 "msg": {"type": "string", "title": "Message"},
463 "type": {"type": "string", "title": "Error Type"},
464 },
465 "type": "object",
466 "required": ["loc", "msg", "type"],
467 "title": "ValidationError",
468 },
469 }
470 },
471 }