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 a4dd474
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 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 All @@ -35,7 +35,7 @@
from pymongo.errors import DuplicateKeyError
from fastapi_users import FastAPIUsers
from beanie import PydanticObjectId
from pydantic import BaseModel
from pydantic import BaseModel, GetCoreSchemaHandler
from kernelci.api.models import (

Check failure on line 39 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

Unable to import 'kernelci.api.models'
Node,
Hierarchy,
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 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 a4dd474

Please sign in to comment.