BeanieODM / beanie

Asynchronous Python ODM for MongoDB
http://beanie-odm.dev/
Apache License 2.0
1.99k stars 211 forks source link

[BUG] 'pydantic_core._pydantic_core.Url' object is not iterable #728

Closed yutongp closed 11 months ago

yutongp commented 11 months ago

Describe the bug Document.save() throws error on pydnticV2.HttpUrl type fields

To Reproduce

from beanie import Document, init_beanie
from pydantic import HttpUrl
from motor.motor_asyncio import AsyncIOMotorClient
import os

class Data(Document):
    link: HttpUrl

MONGO_URI = os.environ.get("MONGO_URI", "mongodb://localhost:27017/")

async def config_db():
    # Create Motor client
    client = AsyncIOMotorClient(MONGO_URI)

    # Init beanie with the Product document class
    await init_beanie(
        database=client["test"],
        document_models=[
            Data,
        ],
    )

await config_db()

data = Data(link="https://www.google.com")
await data.save()

error message:

File [venv/lib/python3.9/site-packages/beanie/odm/utils/encoder.py:244), in Encoder._encode(self, obj)
    243 try:
--> 244     data = dict(obj)
    245 except Exception as e:

TypeError: 'pydantic_core._pydantic_core.Url' object is not iterable

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
File [venv/lib/python3.9/site-packages/beanie/odm/utils/encoder.py:248](venv/lib/python3.9/site-packages/beanie/odm/utils/encoder.py:248), in Encoder._encode(self, obj)
    247 try:
--> 248     data = vars(obj)
    249 except Exception as e:

TypeError: vars() argument must have __dict__ attribute

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
     [24](line=23) await config_db()
     [26](line=25) data = Data(link="https://www.google.com/")
...
    250         errors.append(e)
--> 251         raise ValueError(errors)
    252 return self._encode(data)

ValueError: [TypeError("'pydantic_core._pydantic_core.Url' object is not iterable"), TypeError('vars() argument must have __dict__ attribute')]

Expected behavior save runs without error out

Additional context Add any other context about the problem here.

roman-right commented 11 months ago

Hi! Thank you for the catch - I'll reproduce and fix it this week.

yamanahlawat commented 11 months ago

Also, facing the same issue.

image