BeanieODM / beanie

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

[BUG] `PydanticObjectId` isn't properly configured when deserializing from JSON #940

Closed hongyiweiwu01 closed 2 months ago

hongyiweiwu01 commented 4 months ago

Describe the bug Consider the following code:

class A(BaseModel):
   id: PydanticObjectId

raw = '{"id": "5eb7cf5a86d9755df3a6c593"}'
deserialized = A.model_validate_json(raw)

One would expect type(deserialized.id) == PydanticObjectId, but it's actually deserialized to str.

To Reproduce See example above.

Expected behavior type(deserialized.id) should be PydanticObjectId.

Additional context Note that if I do A.model_validate(json.loads(raw)) the code works. This is potentially because in the __get_pydantic_core_schema__ override in PydanticObjectId, python_schema includes a transformation of the id from str to ObjectId, while the json_schema doesn't and directly uses the built-in str_schema.

github-actions[bot] commented 3 months ago

This issue is stale because it has been open 30 days with no activity.

github-actions[bot] commented 2 months ago

This issue was closed because it has been stalled for 14 days with no activity.