【发布时间】:2023-02-09 03:36:04
【问题描述】:
我有这个装饰器:
def security(required_roles):
def decorator(function):
async def wrapper():
print("ROLES", required_roles)
return function
return wrapper
return decorator
这个端点,我想装饰:
@app.get(
"/me", summary="Get details of currently logged in user", response_model=SystemUser
)
@security(required_roles=["role1", "role2"])
async def get_me(user: SystemUser = Depends(get_current_user)):
return user
但是当我调用它时,我得到了这个:
File "/home/niels/PycharmProjects/fastApiProject/venv/lib/python3.10/site-packages/fastapi/routing.py", line 139, in serialize_response
raise ValidationError(errors, field.type_)
pydantic.error_wrappers.ValidationError: 1 validation error for SystemUser
response
value is not a valid dict (type=type_error.dict)
谁能告诉我为什么以及如何重写装饰器。如果我放置装饰器前@app.get(...) 它没有被执行,也不知道为什么。任何帮助将非常感激。
【问题讨论】:
标签: python fastapi python-decorators