BeanieODM / beanie

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

Serialization Issues with PydanticObjectId and Link in Beanie 1.27.0 [BUG] #1043

Open martincpt opened 2 days ago

martincpt commented 2 days ago

Describe the bug

Since upgrading to Beanie 1.27.0, we've encountered several serialization problems with PydanticObjectId and Link objects.

It seems that PydanticObjectId no longer automatically casts itself to a string primitive during Pydantic serialization.

To Reproduce

Example 1:

class MyDoc(Document):
    related_id: str

MyDoc(related_id=PydanticObjectId())

Ends up with pydantic_core._pydantic_core.ValidationError.

Example 2:

# When testing with FastAPI's TestClient - converting dict to JSON
client.post(endpoint, json={"related_id": PydanticObjectId()})

Ends up with TypeError: Object of type PydanticObjectId is not JSON serializable.

The above snippets worked fine in version 1.26.0 but now fails in 1.27.0.

Expected behavior The expected behavior is that PydanticObjectId should automatically cast itself to a string during Pydantic serialization, as it did in version 1.26.0.

Additional context We traced the issue to the introduction of a new when_used="json" attribute in the __get_pydantic_core_schema__ methods, which seems to be the core cause of the problem.

We acknowledge that our type casting may not have been very strict, but the previous behavior was convenient and worked seamlessly. Without this attribute, all serialization appears to function as expected.

Questions:

  1. What was the rationale behind introducing when_used="json"?
  2. Was this change—specifically the loss of automatic string serialization—intentional, or can we expect a fix in future releases?

Thank you for your hard work and for maintaining such an awesome package!

danielxpander commented 3 hours ago

Same for me. Rollbacked to 1.26.0