odmantic icon indicating copy to clipboard operation
odmantic copied to clipboard

Union of embedded models regression

Open derlikh-smart opened this issue 2 years ago • 0 comments

Bug

I reduced the issue to the smallest I could. It worked on odmantic 0.8.0 and stopped starting on 0.9.0.

from odmantic import Model, EmbeddedModel, Field
from typing import Union


class EmbeddedA(EmbeddedModel):
    type: str = Field("A", const=True)


class EmbeddedB(EmbeddedModel):
    type: str = Field("B", const=True)


class Document(Model):
    embed: Union[EmbeddedA, EmbeddedB]


Document(embed=EmbeddedA(field_a=5)).doc()

Current Behavior

odmantic >= 0.9.0

Traceback (most recent call last):
  File "/private/tmp/odmantic_test/./main.py", line 20, in <module>
    Document(embed=EmbeddedA(field_a=5)).doc()
  File "/private/tmp/odmantic_test/venv/lib/python3.10/site-packages/odmantic/model.py", line 744, in doc
    doc = self.__doc(raw_doc, type(self), include)
  File "/private/tmp/odmantic_test/venv/lib/python3.10/site-packages/odmantic/model.py", line 714, in __doc
    doc[field.key_name] = [
  File "/private/tmp/odmantic_test/venv/lib/python3.10/site-packages/odmantic/model.py", line 715, in <listcomp>
    self.__doc(item, field.model) for item in raw_doc[field_name]
  File "/private/tmp/odmantic_test/venv/lib/python3.10/site-packages/odmantic/model.py", line 722, in __doc
    doc[field.key_name] = raw_doc[field_name]
TypeError: string indices must be integers

Expected behavior

Using 0.8.0 it works.

Environment

  • ODMantic version: 0.9.2
  • MongoDB version: None
  • Pydantic infos (output of python -c "import pydantic.utils; print(pydantic.utils.version_info())):
pydantic version: 1.10.7
            pydantic compiled: True
                 install path: /private/tmp/odmantic_test/venv/lib/python3.10/site-packages/pydantic
               python version: 3.10.4 (main, Jun 13 2022, 11:38:58) [Clang 13.1.6 (clang-1316.0.21.2.5)]
                     platform: macOS-13.2.1-x86_64-i386-64bit
     optional deps. installed: ['typing-extensions']

derlikh-smart avatar Mar 31 '23 07:03 derlikh-smart