-
Notifications
You must be signed in to change notification settings - Fork 56
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
nested pydantic cloudevent serialization doesn't work #198
Comments
that's kinda the "default" behavior, but sure, let's wait for a PR 👍 |
I'm having this issue as well, but in pydantic v2. It works fine in vanilla pydantic, but does not work with the In my specific case, I have a Generic subclass for our models. import uuid
from typing import Generic, TypeVar
from cloudevents.pydantic.v2 import CloudEvent
from pydantic import BaseModel
T = TypeVar("T", bound=BaseModel)
class MyEntity(BaseModel):
id: uuid.UUID
class MyCloudEvent(CloudEvent, Generic[T]):
data: T
if __name__ == "__main__":
obj = MyEntity(id=uuid.uuid4())
evt = MyCloudEvent(
id=str(uuid.uuid4()),
source="urn:source",
subject="subject",
data=obj,
event_type="MyEntity",
type="xyz",
)
# This works:
# print(evt.model_dump(mode="python"))
# But this does not:
print(evt.model_dump(mode="json")) Stack trace snippet:
|
We're using fastapi, so the workaround I'm using for the above nested-object serialization issue is: from fastapi.encoders import jsonable_encoder
jsonable_encoder(event.model_dump(mode="python")) Which works ... but this used to work without these extra hoops in pydantic v1 (and older cloudevents lib). |
We also just ran into the same problem with pydantic v2 and
We now did a dirty workaround of first serializing the event data to json, then loading it back into a Python dictionary and the processing it further, before we emit our event. |
Which is kinda silly because |
will not work
I will fix it myself
The text was updated successfully, but these errors were encountered: