Repository navigation
Conversation
.json() and model_dump_json() sent the payload through stdlib json.dumps, which rejects datetime and the other values Pydantic already knows how to encode. Signed-off-by: eisen0419 <usetoken0419@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related Issue: Fixes #213
Type of change:
Description:
Both Pydantic
CloudEventimplementations (cloudevents.v1.pydantic.v2.eventandcloudevents.v1.pydantic.v1.event, re-exported viacloudevents.v1.pydantic.CloudEvent) serialize structured JSON throughcloudevents.v1.conversion.to_json, which delegates to stdlibjson.dumpswithout applying Pydantic's JSON serializer todata. As a result, calling.json()or.model_dump_json()whendatacontainsdatetime(or other Pydantic-serializable types such asdate,time,UUID,Decimal, or nestedBaseModelinstances) raisedTypeError: Object of type datetime is not JSON serializable.This change encodes non-binary
datapayloads with the corresponding Pydantic serializer prior toconversion.to_jsonwhile leaving top-level binary payloads (bytes,bytearray,memoryview) untouched so they continue to serialize asdata_base64per the CloudEvents JSON format specification:dataviaTypeAdapter(Any).dump_python(..., mode="json")while forwarding relevantSerializationInfooptions (by_alias,exclude_unset,exclude_defaults,exclude_none,exclude_computed_fields,round_trip,serialize_as_any,polymorphic_serialization,context).datausing thedefaultencoder passed intoConfig.json_dumps(including customjson_encodersand.json(encoder=...)) while preserving outerjson.dumpsformatting arguments.timeRFC3339 formatting, null-attribute omission, extensions) andevent.datain-memory values remain unchanged.Pre-submission checklist:
git commit --signoff.README.md,CHANGELOG.md, etc.) as necessary.pre-commitandtoxand all checks pass.