Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e345ba6

Browse files
committed
Fix issue with finding dependency exceptions in ast.AsyncFunctionDef endpoints
1 parent d3b1455 commit e345ba6

4 files changed

Lines changed: 31 additions & 5 deletions

File tree

fastapi_docx/exception_finder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def find_dependency_exceptions(
209209
source = inspect.getsource(func)
210210
tree = ast.parse(source)
211211

212-
for node in find_nodes(tree, ast.FunctionDef):
212+
for node in find_nodes(tree, (ast.FunctionDef, ast.AsyncFunctionDef)):
213213
for kwarg in chain(node.args.defaults, node.args.kw_defaults):
214214
if (
215215
kwarg

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "fastapi-docx"
3-
version = "0.1.7"
3+
version = "0.1.8"
44
description = "Extend a FastAPI OpenAPI spec to include all possible HTTPException or custom Exception response schemas."
55
authors = ["Saran Connolly <[email protected]>"]
66
readme = "README.md"

tests/unit_tests/fixtures/dependencies.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ def __init__(self):
1515
self.bind = None
1616

1717

18+
class User:
19+
def __init__(self, id: int = 1):
20+
self.id = id
21+
22+
1823
class DbDeps(AppDeps):
1924
@staticmethod
2025
def get_db() -> Generator:
@@ -50,6 +55,21 @@ def get_current_user() -> dict[str, Any]:
5055
raise TooManyRequests(context=user)
5156
return user
5257

58+
@staticmethod
59+
async def get_current_user_obj() -> User:
60+
try:
61+
user = User()
62+
except Exception:
63+
raise HTTPException(
64+
status_code=status.HTTP_403_FORBIDDEN,
65+
detail="Could not validate credentials",
66+
)
67+
if not user:
68+
raise HTTPException(status_code=404, detail="User not found")
69+
if user["name"] == "bot":
70+
raise TooManyRequests(context=user)
71+
return user
72+
5373
@staticmethod
5474
def require_auth() -> dict[str, Any]:
5575
try:

tests/unit_tests/test_dependency_exceptions.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1+
from typing import Any
2+
13
from fastapi import APIRouter, Depends, FastAPI
24

35
from tests.unit_tests.fixtures.custom_exceptions import (
46
AppExceptionCase,
57
AppExecptionSchema,
68
app_exception_handler,
79
)
8-
from tests.unit_tests.fixtures.dependencies import AppDeps, CallableDep, UserDeps
10+
from tests.unit_tests.fixtures.dependencies import AppDeps, CallableDep, User, UserDeps
911
from tests.unit_tests.fixtures.services import AppService, UserService
1012
from tests.unit_tests.setup import OpenApiTest
1113

1214
router = APIRouter()
1315

1416

1517
@router.get("/me")
16-
def get_user(*, user_in, current_user=Depends(UserDeps.get_current_user)):
18+
def get_user(
19+
*, user_in, current_user: dict[str, Any] = Depends(UserDeps.get_current_user)
20+
):
1721
result = UserService.get_authenticated(user_in)
1822
return result
1923

2024

2125
@router.put("/me")
22-
def update_user(user_in, current_user=Depends(UserDeps.get_current_user)):
26+
async def update_user(
27+
user_in, current_user: User = Depends(UserDeps.get_current_user_obj)
28+
):
2329
result = UserService.get_authenticated(user_in)
2430
return result
2531

0 commit comments

Comments
 (0)