Skip to content

Commit

Permalink
api.main: fix pydantic v2 warnings for UserGroup
Browse files Browse the repository at this point in the history
Fix the below warnings from pydantic v2 while creating
or updating a user account with user groups:
```
kernelci-api          | /home/kernelci/.local/lib/python3.11/site-packages/pydantic/main.py:390: UserWarning: Pydantic serializer warnings:
kernelci-api          |   Expected `str` but got `UserGroup` with value `UserGroup(id=ObjectId('64...16ad'), name='kernelci')` - serialized value may not be as expected
kernelci-api          |   return self.__pydantic_serializer__.to_python(
```
Signed-off-by: Jeny Sadadia <[email protected]>
  • Loading branch information
Jeny Sadadia authored and JenySadadia committed Nov 27, 2024
1 parent 96245e6 commit 690480c
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@
User,
UserRead,
UserCreate,
UserCreateRequest,
UserUpdate,
UserUpdateRequest,
UserGroup,
)
from .metrics import Metrics
Expand Down Expand Up @@ -157,7 +159,7 @@ def get_current_superuser(user: User = Depends(

@app.post("/user/register", response_model=UserRead, tags=["user"],
response_model_by_alias=False)
async def register(request: Request, user: UserCreate,
async def register(request: Request, user: UserCreateRequest,
current_user: User = Depends(get_current_superuser)):
"""User registration route
Expand All @@ -183,9 +185,11 @@ async def register(request: Request, user: UserCreate,
detail=f"User group does not exist with name: \
{group_name}")
groups.append(group)
user.groups = groups
user_create = UserCreate(**(user.model_dump(
exclude={'groups'}, exclude_none=True)))
user_create.groups = groups
created_user = await register_router.routes[0].endpoint(
request, user, user_manager)
request, user_create, user_manager)
# Update user to be an admin user explicitly if requested as
# `fastapi-users` register route does not allow it
if user.is_superuser:
Expand Down Expand Up @@ -233,7 +237,7 @@ async def register(request: Request, user: UserCreate,

@app.patch("/user/me", response_model=UserRead, tags=["user"],
response_model_by_alias=False)
async def update_me(request: Request, user: UserUpdate,
async def update_me(request: Request, user: UserUpdateRequest,
current_user: User = Depends(get_current_user)):
"""User update route
Expand All @@ -258,15 +262,17 @@ async def update_me(request: Request, user: UserUpdate,
detail=f"User group does not exist with name: \
{group_name}")
groups.append(group)
user_update = UserUpdate(**(user.model_dump(
exclude={'groups'}, exclude_none=True)))
if groups:
user.groups = groups
user_update.groups = groups
return await users_router.routes[1].endpoint(
request, user, current_user, user_manager)
request, user_update, current_user, user_manager)


@app.patch("/user/{user_id}", response_model=UserRead, tags=["user"],
response_model_by_alias=False)
async def update_user(user_id: str, request: Request, user: UserUpdate,
async def update_user(user_id: str, request: Request, user: UserUpdateRequest,
current_user: User = Depends(get_current_superuser)):
"""Router to allow admin users to update other user account"""
metrics.add('http_requests_total', 1)
Expand Down Expand Up @@ -295,11 +301,14 @@ async def update_user(user_id: str, request: Request, user: UserUpdate,
detail=f"User group does not exist with name: \
{group_name}")
groups.append(group)
user_update = UserUpdate(**(user.model_dump(
exclude={'groups'}, exclude_none=True)))

if groups:
user.groups = groups
user_update.groups = groups

updated_user = await users_router.routes[3].endpoint(
user, request, user_from_id, user_manager
user_update, request, user_from_id, user_manager
)
# Update user to be an admin user explicitly if requested as
# `fastapi-users` user update route does not allow it
Expand Down

0 comments on commit 690480c

Please sign in to comment.