Skip to content

Commit 86d007b

Browse files
Merge pull request #225 from piercefreeman/feature/fix-fastapi-dep-injection
Compatibility with fastapi 0.118
2 parents 54bcc27 + 0a14ebc commit 86d007b

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

mountaineer/dependencies/base.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,6 @@ async def get_function_dependencies(
7676
# Synthesize defaults
7777
if not url:
7878
url = "/synthetic"
79-
if not request:
80-
request = Request(
81-
scope={
82-
"type": "http",
83-
"path": url,
84-
"path_params": {},
85-
"query_string": "",
86-
"headers": [],
87-
}
88-
)
8979

9080
# Synthetic request object as if we're coming from the original first page
9181
dependant = get_dependant(
@@ -94,6 +84,28 @@ async def get_function_dependencies(
9484
)
9585

9686
async with AsyncExitStack() as async_exit_stack:
87+
# FastAPI 0.118+ requires these exit stacks in the request scope for
88+
# dependency resolution. We provide them for backwards compatibility.
89+
if not request:
90+
request = Request(
91+
scope={
92+
"type": "http",
93+
"path": url,
94+
"path_params": {},
95+
"query_string": "",
96+
"headers": [],
97+
# FastAPI 0.118+ scope requirements
98+
"fastapi_inner_astack": async_exit_stack,
99+
"fastapi_function_astack": async_exit_stack,
100+
}
101+
)
102+
else:
103+
# Inject into existing request scope if not present
104+
if "fastapi_inner_astack" not in request.scope:
105+
request.scope["fastapi_inner_astack"] = async_exit_stack
106+
if "fastapi_function_astack" not in request.scope:
107+
request.scope["fastapi_function_astack"] = async_exit_stack
108+
97109
payload = await solve_dependencies(
98110
request=request,
99111
dependant=dependant,

0 commit comments

Comments
 (0)