-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfastapi_custom.py
73 lines (51 loc) · 2.46 KB
/
fastapi_custom.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
""""""
from typing import Callable
from fastapi import FastAPI
from fastapi.routing import APIRoute
########################################################################################################################
# Source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
ALL_HTTP_METHODS = ["CONNECT", "DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT", "TRACE"]
########################################################################################################################
def use_route_names_as_operation_ids(app: FastAPI) -> None:
"""
Simplify operation IDs so that generated API clients have simpler function
names.
Should be called only after all routes have been added.
Author: Wouter Vermeulen 2024-01-19
Source: https://fastapi.tiangolo.com/advanced/path-operation-advanced-configuration/#using-the-path-operation-function-name-as-the-operationid
"""
for route in app.routes:
if isinstance(route, APIRoute):
route.operation_id = route.name
########################################################################################################################
def hide_422(func: Callable) -> Callable:
"""
Indicate to `hide_default_responses` that the '422 parsing error' from pydantic should be hidden in the openapi
documentation for the function passed as `func` (this function is meant to be used as a decorator: `@hide_422`.
Author: Wouter Vermeulen
2024-01-19
"""
func.__hide_422__ = True
return func
def hide_default_responses(app: FastAPI) -> None:
"""
Hide the responses as indicated with the function attributes `__hide_{status_code}__`.
Slighly based on: https://github.com/tiangolo/fastapi/issues/497#issuecomment-546752110.
Author: Wouter Vermeulen
2024-01-19
"""
def delete_response(route, paths, response: str):
methods = route.methods or ["GET"]
for method in methods:
try:
paths[route.path][method.lower()]["responses"].pop(response)
except KeyError:
pass
openapi_schema = app.openapi()
paths = openapi_schema["paths"]
for route in app.routes:
if not isinstance(route, APIRoute):
continue
for response_status_code in [422]: # Extend to other status codes here
if getattr(route.endpoint, f"__hide_{response_status_code}__", None):
delete_response(route, paths, str(response_status_code))