`.json()` unable to serialize datetime elements in `data` payload
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- python
- Domain
- backend-api-design
Research direction
The issue identifies cloudevents.pydantic.CloudEvent.json() as the entry point; start by comparing its datetime handling with Pydantic BaseModel.json(). Add a regression case using datetime in data and verify that serialization produces a string rather than raising. Confirm the behavior with the reproduction shown in the issue.
Written by the indexing model from the issue text.
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
dataargument is adatetimeinstance. - 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
- Dominant language
- Python
- Stars
- 342
- Forks
- 65
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from cloudevents/sdk-python
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
cloudevents/sdk-python#304 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
cloudevents/sdk-python#300 ·
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
cloudevents/sdk-python#291 · 8 comments ·
-
Difficulty 1/5 Under an hour Newbie friendliness 48/100
cloudevents/sdk-python#247 · 2 comments ·
-
question
Difficulty 3/5 1-2 days Newbie friendliness 45/100
cloudevents/sdk-python#246 · 5 comments ·
All issues in cloudevents/sdk-python
Similar issues
-
Add: hunch Open
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
AbdelStark/awesome-typesafe#104 ·
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
DiamondLightSource/dodal#2211 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
openml/openml-python#1749 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
sipyourdrink-ltd/bernstein#6191 ·