【问题标题】:How to change description of a include_router?如何更改 include_router 的描述?
【发布时间】:2022-11-10 12:18:41
【问题描述】:

我试图将新描述传递给 include_router,但它不接受 [description="description"]。

下面您会看到带有自定义描述的自定义工作路线。

from fastapi import Depends
from sqlalchemy import select
from fastapi import APIRouter, FastAPI
from app.schemas.schemas import UserRead
from app.routes.permissions import admin_route
from sqlalchemy.ext.asyncio import AsyncSession
from app.schemas.schemas import UserCreate, UserUpdate
from app.models.users import auth_backend, fastapi_users
from app.databases.user import User, get_async_session

test = APIRouter()


test.include_router(
    fastapi_users.get_users_router(UserRead, UserUpdate),
    prefix="users",
    tags=["stuff"],
)



@test.get("users", response_model=list[UserRead], tags=["stuff"], description="description")
async def method(session: AsyncSession = Depends(get_async_session)):
    statement = select(User)
    result = await session.execute(statement)
    return result.scalars().all()

有没有办法在不改变插件代码的情况下改变 fastapi-users 中的标准路由行为?

感谢帮助。

【问题讨论】:

    标签: fastapiusers


    【解决方案1】:

    明白了,这是一个解决方案,我循环遍历默认路由并将它们与定义的数组进行比较,以将更改应用于默认路由数组中的特定位置:

    from app.schemas.schemas import UserRead
    from fastapi import APIRouter
    from app.schemas import UserCreate, UserUpdate
    from app.users import auth_backend, fastapi_users
    
    app_router = APIRouter()
    
    
    app_router.include_router(
        fastapi_users.get_auth_router(auth_backend),
        tags=["auth"],
        prefix="/auth/jwt",
    )
    
    app_router.include_router(
        fastapi_users.get_register_router(UserRead, UserCreate),
        prefix="/auth",
        tags=["auth"],
    )
    
    app_router.include_router(
        fastapi_users.get_users_router(UserRead, UserUpdate),
        prefix="/users",
        tags=["users"],
    )
    
    
    for x in ("users:patch_current_user", "users:current_user"):
        app_router.routes.remove([y for y in app_router.routes if y.name == x][0])
    
    
    route_desired_content = [["auth:jwt.login", "User login to get access to to protected endpoints", "User Login"],
                             ["auth:jwt.logout", "User logout", "User Logout"],
                             ["register:register", "Registers new active users in the database", "Create new user"],
                             ["users:user", "Gets information from the database about a specific user by id", "Get user data"],
                             ["users:patch_user", "Changes all information in the database from a specific user by id", "Update user data"],
                             ["users:delete_user", "Deletes a specific user by id from the database", "Delete User"]]
    
    for x in range(0, len(route_desired_content)):
        route_name = app_router.routes[x].name
        for z in route_desired_content:
            if route_name == z[0]:
                app_router.routes[x].description = z[1]
                app_router.routes[x].name = z[2]
    

    所有默认路由都设置在 route_desired_content 数组中,除了上面一步删除的两个。

    【讨论】:

      猜你喜欢
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多