Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pydantic v2 warnings related to UserGroup #572

Merged

Conversation

JenySadadia
Copy link
Collaborator

Related to #560

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(

Fix the issue by defining and using different user models for API endpoint and fastapi-users router, i.e. different
models as request schema for API request model and schema for request to fastapi-users router.
For create user request, user needs to send UserCreateRequest schema as API router request
and the router will use UserCreate schema to send the request to fastapi-users router.
This is due to, we intend to have a list of user group name strings as API router request and UserGroup instance as fastapi-users router request as user.groups field value.

Jeny Sadadia added 2 commits November 26, 2024 16:57
Define and use different user models for API
endpoint and fastapi-users router, i.e. different
models as request schema for API request model and schema
for request to `fastapi-users` router.
For create user request, user needs to send
`UserCreateRequest` schema as API router request
and the router will use `UserCreate` schema to
send the request to `fastapi-users` router.
This is due to we intend to have a list of user group
name strings as API router request and `UserGroup`
instance as `fastapi-users` router request as `user.groups`
field value.

Signed-off-by: Jeny Sadadia <[email protected]>
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]>
@JenySadadia JenySadadia force-pushed the fix-pydantic-usergroup-warnings branch from 514774d to 35cdfa5 Compare November 26, 2024 11:28
Copy link
Contributor

@pawiecz pawiecz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

(a child task to start reusing field validators might be useful - let me know what you think)

Field(default=None)]
groups: List[UserGroup] = Field(default=[])

@field_validator('groups')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be convenient to reuse field validators but such discussion has never been resolved

Maybe the possible approaches to this task could be explored in future

Copy link
Collaborator Author

@JenySadadia JenySadadia Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the input. Created #579

@JenySadadia JenySadadia added this pull request to the merge queue Nov 27, 2024
Merged via the queue into kernelci:main with commit 690480c Nov 27, 2024
6 checks passed
@JenySadadia JenySadadia deleted the fix-pydantic-usergroup-warnings branch November 27, 2024 14:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants