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

Can't save a model with a field of type pydantic.AnyUrl #478

Open
jmensch1 opened this issue May 20, 2024 · 0 comments
Open

Can't save a model with a field of type pydantic.AnyUrl #478

jmensch1 opened this issue May 20, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@jmensch1
Copy link

Bug

Can't save a model with a field of type pydantic.AnyUrl.

Current Behavior

from odmantic import Model
from pydantic import AnyUrl

from api.models import db


class User(Model):
    name: str


class UserWithPicture(Model):
    name: str
    picture: AnyUrl


async def test_urls() -> None:
    # saves user as expected
    user = User(name="tom")
    await db.save(user)

    # add a url and it throws an error
    user_with_picture = UserWithPicture(
        name="harry",
        picture="https://picsum.photos/200/300",
    )
    await db.save(user_with_picture)

if __name__ == "__main__":
    import asyncio

    loop = asyncio.get_event_loop()
    loop.run_until_complete(test_urls())

Output:

bson.errors.InvalidDocument: cannot encode object: Url('https://picsum.photos/200/300'), of type: <class 'pydantic_core._pydantic_core.Url'>

Expected behavior

I'm upgrading from pydantic v1/odmantic 0.8.0 to pydantic v2/odmantic 1.0.2. Saving urls was working before the upgrade, and I'd expect it to continue working.

Environment

  • ODMantic version: 1.0.2
  • MongoDB version: 6
  • Pydantic infos (output of python -c "import pydantic.utils; print(pydantic.utils.version_info())):
pydantic version: 2.7.1
pydantic-core version: 2.18.2
pydantic-core build: profile=release pgo=false
python version: 3.8.12 (default, Sep 16 2021, 13:23:36)  [Clang 12.0.5 (clang-1205.0.22.11)]
platform: macOS-14.2-x86_64-i386-64bit
related packages: pydantic-extra-types-2.4.0 typing_extensions-4.11.0 mypy-0.931 fastapi-0.111.0 pydantic-settings-2.1.0
commit: unknown

@jmensch1 jmensch1 added the bug Something isn't working label May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant