Skip to content

Commit

Permalink
fix(main.py): Custom JSON Schema for PydanticObjectId
Browse files Browse the repository at this point in the history
Attempt to fix pydantic serialization errors

Signed-off-by: Denys Fedoryshchenko <[email protected]>
  • Loading branch information
nuclearcat committed Dec 13, 2024
1 parent a68ee8a commit 806cfa3
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import re
import asyncio
import traceback
from typing import List, Union, Optional
from typing import List, Union, Optional, Any
from datetime import datetime
from contextlib import asynccontextmanager
from fastapi import (
Expand Down Expand Up @@ -82,8 +82,22 @@ async def lifespan(app: FastAPI): # pylint: disable=redefined-outer-name
auth = Authentication(token_url="user/login")
pubsub = None # pylint: disable=invalid-name


class CustomObjectId(PydanticObjectId):

Check warning on line 86 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

Missing class docstring
@classmethod
def __get_pydantic_json_schema__(cls, core_schema: Any, handler: GetCoreSchemaHandler) -> dict[str, Any]:

Check warning on line 88 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

Line too long (109/100)

Check warning on line 88 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

Parameter 'schema' has been renamed to 'core_schema' in overridden 'CustomObjectId.__get_pydantic_json_schema__' method

Check failure on line 88 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

Undefined variable 'GetCoreSchemaHandler'

Check warning on line 88 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

line too long (109 > 79 characters)
json_schema = handler(core_schema)
# Indicate that this field should be treated like a string
# with a pattern matching a 24-character hex string
json_schema.update({
"type": "string",
"pattern": "^[0-9a-fA-F]{24}$"
})
return json_schema


auth_backend = auth.get_user_authentication_backend()
fastapi_users_instance = FastAPIUsers[User, PydanticObjectId](
fastapi_users_instance = FastAPIUsers[User, CustomObjectId](
get_user_manager,
[auth_backend],
)
Expand Down

0 comments on commit 806cfa3

Please sign in to comment.