Skip to content

Commit 1469a96

Browse files
committed
Add fastapi source
1 parent 9669409 commit 1469a96

3 files changed

Lines changed: 37 additions & 18 deletions

File tree

aikido_zen/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def protect(mode="daemon", token=""):
6161
import aikido_zen.sources.flask
6262
import aikido_zen.sources.quart
6363
import aikido_zen.sources.starlette
64+
import aikido_zen.sources.fastapi
6465
import aikido_zen.sources.xml_sources.xml
6566
import aikido_zen.sources.xml_sources.lxml
6667

aikido_zen/sources/fastapi.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from aikido_zen.sinks import on_import, patch_function
2+
from aikido_zen.sources.starlette.starlette_routing import _request_response
3+
4+
5+
@on_import("fastapi.routing", "fastapi")
6+
def patch(m):
7+
"""
8+
FastAPI defines its own request_response in fastapi/routing.py rather than
9+
importing it from starlette.routing. Patching only starlette.routing.request_response
10+
leaves all FastAPI APIRoute endpoints unwrapped, so pre_response and post_response
11+
hooks never fire for those routes.
12+
(github src: https://github.com/tiangolo/fastapi/blob/master/fastapi/routing.py)
13+
"""
14+
patch_function(m, "request_response", _request_response)

end2end/fastapi_postgres_uvicorn_test.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,26 +88,30 @@ def test_routes_discovered_in_heartbeat():
8888

8989
validate_heartbeat(
9090
heartbeat_events[0],
91-
routes=[{
92-
"apispec": {
93-
"body": {
94-
"type": "form-urlencoded",
95-
"schema": {
96-
"type": "object",
97-
"properties": {
98-
"dog_name": {
99-
"items": {"type": "string"},
100-
"type": "array",
101-
}
91+
routes=[
92+
{
93+
"apispec": {
94+
"auth": None,
95+
"body": {
96+
"schema": {
97+
"properties": {"dog_name": {"type": "string"}},
98+
"type": "object",
10299
},
100+
"type": "form-urlencoded",
103101
},
102+
"query": None,
104103
},
105-
"query": None,
106-
"auth": None,
104+
"hits": 2,
105+
"hits_delta_since_sync": 0,
106+
"method": "POST",
107+
"path": "/create",
107108
},
108-
"hits": 1,
109-
"hits_delta_since_sync": 0,
110-
"method": "POST",
111-
"path": "/create",
112-
}],
109+
{
110+
"apispec": {},
111+
"hits": 1,
112+
"hits_delta_since_sync": 0,
113+
"method": "GET",
114+
"path": "/sync_route",
115+
},
116+
],
113117
)

0 commit comments

Comments
 (0)