-
Notifications
You must be signed in to change notification settings - Fork 235
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
Labels
bug
Something isn't working
Comments
This issue is stale because it has been open 30 days with no activity. |
This issue was closed because it has been stalled for 14 days with no activity. |
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
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
Describe the bug
Consider the following code:
One would expect
type(deserialized.id) == PydanticObjectId
, but it's actually deserialized tostr
.To Reproduce
See example above.
Expected behavior
type(deserialized.id)
should bePydanticObjectId
.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 inPydanticObjectId
,python_schema
includes a transformation of the id fromstr
toObjectId
, while thejson_schema
doesn't and directly uses the built-instr_schema
.The text was updated successfully, but these errors were encountered: