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 encoding keys in Mapping branch of Encoder #785

Merged
merged 4 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion beanie/odm/utils/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def encode(self, obj: Any) -> Any:
items = self._iter_model_items(obj)
return {key: self.encode(value) for key, value in items}
if isinstance(obj, Mapping):
return {key: self.encode(value) for key, value in obj.items()}
return {str(key): self.encode(value) for key, value in obj.items()}
if isinstance(obj, Iterable):
return [self.encode(value) for value in obj]

Expand Down
2 changes: 2 additions & 0 deletions tests/odm/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
DocumentWithBackLink,
DocumentWithBsonBinaryField,
DocumentWithBsonEncodersFiledsTypes,
DocumentWithComplexDictKey,
DocumentWithCustomFiledsTypes,
DocumentWithCustomIdInt,
DocumentWithCustomIdUUID,
Expand Down Expand Up @@ -277,6 +278,7 @@ async def init(db):
DocWithCallWrapper,
DocumentWithOptionalBackLink,
DocumentWithOptionalListBackLink,
DocumentWithComplexDictKey,
]
await init_beanie(
database=db,
Expand Down
4 changes: 4 additions & 0 deletions tests/odm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,3 +1048,7 @@ def foo(self, bar: str) -> None:

class DocumentWithHttpUrlField(Document):
url_field: HttpUrl


class DocumentWithComplexDictKey(Document):
dict_field: Dict[UUID, datetime.datetime]
17 changes: 17 additions & 0 deletions tests/odm/test_encoder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
from datetime import date, datetime
from uuid import uuid4

import pytest
from bson import Binary, Regex
Expand All @@ -11,6 +12,7 @@
Child,
DocumentForEncodingTest,
DocumentForEncodingTestDate,
DocumentWithComplexDictKey,
DocumentWithDecimalField,
DocumentWithHttpUrlField,
DocumentWithKeepNullsFalse,
Expand Down Expand Up @@ -152,3 +154,18 @@ async def test_should_be_able_to_save_retrieve_doc_with_url():

assert isinstance(new_doc.url_field, AnyUrl)
assert new_doc.url_field == doc.url_field


async def test_dict_with_complex_key():
assert isinstance(Encoder().encode({uuid4(): datetime.now()}), dict)

uuid = uuid4()
# reset microseconds, because it looses by mongo
dt = datetime.now().replace(microsecond=0)

doc = DocumentWithComplexDictKey(dict_field={uuid: dt})
await doc.insert()
new_doc = await DocumentWithComplexDictKey.get(doc.id)

assert isinstance(new_doc.dict_field, dict)
assert new_doc.dict_field.get(uuid) == dt