Debian Codegraph @

tests/benchmarks/utils.py

raw
1 from collections.abc import Callable
2 from typing import Annotated, Any
3
4 from fastapi import Depends, FastAPI
5
6 LAST_DEPENDENCY_INDEX = 100
7 ROUTE_COUNT = 20
8 ROUTE_PATH_PREFIX = "/openapi-route-"
9
10
11 def create_openapi_app() -> FastAPI:
12 app = FastAPI()
13 dependencies: dict[int, Callable[..., Any]] = {}
14
15 def create_dependency(index: int) -> Callable[..., Any]:
16 if index == LAST_DEPENDENCY_INDEX:
17
18 def dependency(query_value: int = index) -> str:
19 return str(query_value)
20
21 dependency.__name__ = f"dependency_{index}"
22 return dependency
23
24 next_dependency = dependencies[index + 1]
25
26 async def dependency(
27 sub_dependency: Annotated[str, Depends(next_dependency)],
28 query_value: int = index,
29 ) -> str:
30 return f"{query_value} -> {sub_dependency}"
31
32 dependency.__name__ = f"dependency_{index}"
33 return dependency
34
35 for index in reversed(range(LAST_DEPENDENCY_INDEX + 1)):
36 dependencies[index] = create_dependency(index)
37
38 async def endpoint(
39 value: Annotated[str, Depends(dependencies[0])],
40 ) -> dict[str, str]:
41 return {"value": value}
42
43 for index in range(ROUTE_COUNT):
44 app.add_api_route(f"{ROUTE_PATH_PREFIX}{index}", endpoint, methods=["GET"])
45
46 return app
47
48
49 def generate_openapi(app: FastAPI) -> dict[str, Any]:
50 app.openapi_schema = None
51 return app.openapi()