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

Fix PydanticObjectId fields being parsed into str when using Pydantic's model_validate_json #1054

Closed
wants to merge 2 commits into from

Conversation

ivan-gj
Copy link
Contributor

@ivan-gj ivan-gj commented Oct 14, 2024

Unsure if this could be fixed in a more stylish fashion, but it was simply incorrectly setup before, at least now it works.

Please make as many changes as needed if this solution could be improved.

I believe this bugfix is quite important. If you parse an ID (let's call the variable identifier) from a JSON file in order to look it up on your Mongo database... you won't find it.

 await cls.find_one(cls.id == identifier)

will return None even if the object exists, because identifier will be of type str instead of ObjectId when parsing from a JSON file with Pydantic's model_validate_json.

Fixes #940.

…tic's `model_validate_json`

Unsure if this could be fixed in a more stylish fashion, but it was simply incorrectly setup before, at least now it works. 

**Please make as many changes as needed if this solution could be improved.**

I believe this bugfix is quite important. If you parse an ID (let's call the variable `identifier`) from a JSON file in order to look it up on your Mongo database... you won't find it.

```python
 await cls.find_one(cls.id == identifier)
```
will return `None` even if the object exists, because `identifier` will be of type `str` instead of `ObjectId` when parsing from a JSON file with Pydantic's `model_validate_json`. 

Fixes BeanieODM#940.
@adeelsohailahmed adeelsohailahmed requested review from 07pepa and a team October 17, 2024 18:25
@staticxterm
Copy link

Hi @ivan-gj, thank you for your contribution.
So this only happens with Pydantic v2, right? Could you please add an example test case that was failing before, and is passing now (to cover this regression)? E.g. like the code from #940?

@07pepa
Copy link
Member

07pepa commented Oct 21, 2024

@ivan-gj Thanks for your contribution. There is some undocumented pydantic stuff that i had to figure out with brute force in my pr #1060 I wanted to help but ended up doing whole PR

closing as duplicate

@07pepa 07pepa closed this Oct 21, 2024
@ivan-gj
Copy link
Contributor Author

ivan-gj commented Oct 21, 2024

No problem! Glad to see it getting fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] PydanticObjectId isn't properly configured when deserializing from JSON
3 participants