fastapiusers

How to change description of a include_router?


im trying to pass a new description to the include_router, but it doesn`t accept [description="description"].

Below you see the selfdefined working route with a custom 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()

Is there a way to change the standard routes behavoir in fastapi-users, without altering the plugins code?

Thanks for help.


Solution

  • Got it, here is a solution where i loop through the default routes and compare them with a defined array to apply the changes on the specific position in the default routes array:

    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]
    

    All default routes are set in the route_desired_content array except of the two which were removed one step above.