wallneradam / esorm

Python ElasticSearch ORM based on Pydantic
https://esorm.readthedocs.io/
Mozilla Public License 2.0
35 stars 0 forks source link

Pydantic HttpUrl doesn't work #14

Closed heindrickdumdum0217 closed 3 months ago

heindrickdumdum0217 commented 3 months ago

Environment

Pydantic == 2.8.2
pyesorm == 0.5.0

Got the following error.

errors=(
  TypeError(
    "Unable to serialize Url('https://xxx.yyy.com/images/thumbnails/114496/d3uHiMWrKfQufRvPe6cC9Ttcr9rc2e0Amzi5boS1') (type: <class 'pydantic_core._pydantic_core.Url'>)")
  )
)
wallneradam commented 3 months ago

Could you please share your model, which cause the problem? Becase I have test for a model with HttpUrl and it works.

heindrickdumdum0217 commented 3 months ago
Class User(ESModel):
    avatar_image_url: HttpUrl

class Comment(ESModel):
    id: unsigned_long
    url: HttpUrl
    user: User

I think the error happens in User model.

wallneradam commented 3 months ago

I've just tested:

async def test():
    import esorm
    from pydantic import HttpUrl
    from esorm.fields import unsigned_long

    class User(esorm.ESModel):
        avatar_image_url: HttpUrl

    class Comment(esorm.ESModel):
        id: unsigned_long
        url: HttpUrl
        user: User

    await esorm.setup_mappings()

    doc = Comment(id=1, url='http://example.com', 
                  user=User(avatar_image_url='https://xxx.yyy.com/images/thumbnails/114496'))
    doc_id = await doc.save()
    assert doc_id is not None

This just works.

Are you sure you using v0.5.0?

wallneradam commented 3 months ago

Can I close this issue?

heindrickdumdum0217 commented 3 months ago

It still throws bugs, but I'm not sure it's Pydantic bug or esorm issue. As my brief checking, it happens when receive encoded urls.

wallneradam commented 3 months ago

Ok, I close this for now, as I think it is fixed in the new version according to my tests. I used exactly your model, and it works.