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

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

Open
hongyiweiwu01 opened this issue May 27, 2024 · 3 comments · May be fixed by #1060
Open

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

hongyiweiwu01 opened this issue May 27, 2024 · 3 comments · May be fixed by #1060
Labels
bug Something isn't working

Comments

@hongyiweiwu01
Copy link

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.

Copy link
Contributor

github-actions bot commented Jul 1, 2024

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

@github-actions github-actions bot added the Stale label Jul 1, 2024
Copy link
Contributor

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

@ivan-gj
Copy link
Contributor

ivan-gj commented Oct 14, 2024

Hello. This bug exists. It's quite critical, to be honest. Please reopen.

ivan-gj added a commit to ivan-gj/beanie that referenced this issue Oct 14, 2024
…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.
@07pepa 07pepa reopened this Oct 21, 2024
@07pepa 07pepa added bug Something isn't working and removed Stale labels Oct 21, 2024
07pepa added a commit to 07pepa/beanie that referenced this issue Oct 21, 2024
07pepa added a commit to 07pepa/beanie that referenced this issue Oct 21, 2024
07pepa added a commit to 07pepa/beanie that referenced this issue Oct 21, 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
3 participants