From 57c03b3a2b61196f60ccfc6b1ba4939c81e38a07 Mon Sep 17 00:00:00 2001 From: tarsil Date: Mon, 20 Nov 2023 16:57:35 +0000 Subject: [PATCH 1/2] Fix UUID representation --- mongoz/core/connection/collections.py | 5 +++++ mongoz/core/db/documents/metaclasses.py | 2 ++ mongoz/core/db/fields/core.py | 1 - 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/mongoz/core/connection/collections.py b/mongoz/core/connection/collections.py index fc968dd..30806d1 100644 --- a/mongoz/core/connection/collections.py +++ b/mongoz/core/connection/collections.py @@ -1,3 +1,4 @@ +from bson.binary import UuidRepresentation from motor.motor_asyncio import AsyncIOMotorCollection @@ -9,3 +10,7 @@ class Collection: def __init__(self, name: str, collection: AsyncIOMotorCollection) -> None: self._collection: AsyncIOMotorCollection = collection self.name = name + self.set_uuid_representation(UuidRepresentation.STANDARD) + + def set_uuid_representation(self, representation: int) -> None: + self._collection.codec_options.uuid_representation = representation diff --git a/mongoz/core/db/documents/metaclasses.py b/mongoz/core/db/documents/metaclasses.py index 232a817..d05691c 100644 --- a/mongoz/core/db/documents/metaclasses.py +++ b/mongoz/core/db/documents/metaclasses.py @@ -15,6 +15,7 @@ no_type_check, ) +from bson import UuidRepresentation from pydantic._internal._model_construction import ModelMetaclass from mongoz.core.connection.collections import Collection @@ -319,6 +320,7 @@ def __search_for_fields(base: Type, attrs: Any) -> None: new_class.__db_document__ = True meta.collection = meta.database.get_collection(collection_name) + meta.collection._collection.codec_options.uuid_representation = UuidRepresentation.STANDARD mongoz_fields: Dict[str, MongozField] = {} for field_name, field in new_class.model_fields.items(): diff --git a/mongoz/core/db/fields/core.py b/mongoz/core/db/fields/core.py index c204328..3fc704a 100644 --- a/mongoz/core/db/fields/core.py +++ b/mongoz/core/db/fields/core.py @@ -349,7 +349,6 @@ def __new__(cls, **kwargs: Any) -> BaseField: # type: ignore **kwargs, **{k: v for k, v in locals().items() if k not in CLASS_DEFAULTS}, } - return super().__new__(cls, **kwargs) From 12f36ce669cea4267ef1cb9b963aa8b45693c6d2 Mon Sep 17 00:00:00 2001 From: tarsil Date: Mon, 20 Nov 2023 17:35:02 +0000 Subject: [PATCH 2/2] Fix codec_options --- mongoz/core/connection/collections.py | 5 ----- mongoz/core/db/documents/metaclasses.py | 2 -- 2 files changed, 7 deletions(-) diff --git a/mongoz/core/connection/collections.py b/mongoz/core/connection/collections.py index 30806d1..fc968dd 100644 --- a/mongoz/core/connection/collections.py +++ b/mongoz/core/connection/collections.py @@ -1,4 +1,3 @@ -from bson.binary import UuidRepresentation from motor.motor_asyncio import AsyncIOMotorCollection @@ -10,7 +9,3 @@ class Collection: def __init__(self, name: str, collection: AsyncIOMotorCollection) -> None: self._collection: AsyncIOMotorCollection = collection self.name = name - self.set_uuid_representation(UuidRepresentation.STANDARD) - - def set_uuid_representation(self, representation: int) -> None: - self._collection.codec_options.uuid_representation = representation diff --git a/mongoz/core/db/documents/metaclasses.py b/mongoz/core/db/documents/metaclasses.py index d05691c..232a817 100644 --- a/mongoz/core/db/documents/metaclasses.py +++ b/mongoz/core/db/documents/metaclasses.py @@ -15,7 +15,6 @@ no_type_check, ) -from bson import UuidRepresentation from pydantic._internal._model_construction import ModelMetaclass from mongoz.core.connection.collections import Collection @@ -320,7 +319,6 @@ def __search_for_fields(base: Type, attrs: Any) -> None: new_class.__db_document__ = True meta.collection = meta.database.get_collection(collection_name) - meta.collection._collection.codec_options.uuid_representation = UuidRepresentation.STANDARD mongoz_fields: Dict[str, MongozField] = {} for field_name, field in new_class.model_fields.items():