From d71cbc928b3855174049f8d8f49be30f41dd0b22 Mon Sep 17 00:00:00 2001 From: slorello89 Date: Mon, 6 May 2024 09:04:30 -0400 Subject: [PATCH] fixing issue with breaking typing.Dict in JsonModel --- aredis_om/model/model.py | 5 +++++ tests/test_json_model.py | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/aredis_om/model/model.py b/aredis_om/model/model.py index 56c5c90d..9270f830 100644 --- a/aredis_om/model/model.py +++ b/aredis_om/model/model.py @@ -83,6 +83,8 @@ def get_outer_type(field): field.annotation ): return field.annotation + elif not hasattr(field.annotation, "__args__"): + return None else: return field.annotation.__args__[0] @@ -1943,6 +1945,9 @@ def schema_for_fields(cls): for name, field in fields.items(): _type = get_outer_type(field) + if _type is None: + continue + if ( not isinstance(field, FieldInfo) and hasattr(field, "metadata") diff --git a/tests/test_json_model.py b/tests/test_json_model.py index d5744858..6b9131c0 100644 --- a/tests/test_json_model.py +++ b/tests/test_json_model.py @@ -971,3 +971,27 @@ async def test_xfix_queries(m): result = await m.Member.find(m.Member.bio % "*ack*").first() assert result.first_name == "Steve" + + + +@py_test_mark_asyncio +async def test_model_with_dict(): + class EmbeddedJsonModelWithDict(EmbeddedJsonModel): + dict: Dict + class ModelWithDict(JsonModel): + embedded_model: EmbeddedJsonModelWithDict + info: Dict + + await Migrator().run() + d = dict() + inner_dict = dict() + d["foo"] = "bar" + inner_dict['bar']='foo' + embedded_model = EmbeddedJsonModelWithDict(dict=inner_dict) + item = ModelWithDict(info=d, embedded_model=embedded_model) + await item.save() + + rematerialized = await ModelWithDict.find(ModelWithDict.pk == item.pk).first() + assert rematerialized.pk == item.pk + assert rematerialized.info['foo'] == 'bar' + assert rematerialized.embedded_model.dict['bar'] == 'foo'