Open
Description
Expected Behavior
The CloudEvent model, like Pydantic's BaseModel, can serialize of datetime
properties to strings in the .json()
function.
Actual Behavior
The CloudEvent model raises a ValueError
from the standard library's json.dumps
function when .json()
is called on a model containing datetime
properties in the data
field.
Alternatively, if this is intentional behavior, it would be helpful to have some guidance around what the expected usage is when users want to have dates or timestamps in their CloudEvent data.
Steps to Reproduce the Problem
- Init a CloudEvent where some element of the
data
argument is adatetime
instance. - Call
.json()
on the CloudEvent instance
For example:
from datetime import datetime
from cloudevents.pydantic import CloudEvent
event = CloudEvent(attributes={'source': 'my.source', 'type': 'com.my.type'}, data={'dt': datetime.now()})
# raises TypeError
print(event.json())
By contrast, Pydantic's BaseModel is able to encode datetime properties:
from datetime import datetime
from pydantic import BaseModel
class MyModel(BaseModel):
dt: datetime
foo = MyModel(dt=datetime.now())
# encodes dt as a string
print(foo.json())
Specifications
- Platform: MacOS Ventura 13.2.1 (Intel)
- Python Version: 3.9.9
Metadata
Metadata
Assignees
Labels
No labels