Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Pydantic `CloudEvent.json()` and `model_dump_json()` now encode `datetime`,
`date`, `time`, `UUID`, `Decimal`, and nested models inside `data`. Binary
payloads still use `data_base64`.

## [2.2.0]

### Changed
Expand Down
31 changes: 30 additions & 1 deletion src/cloudevents/v1/pydantic/v1/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@
from cloudevents.v1.sdk.event import attribute


def _jsonable_data( # type: ignore[no-untyped-def]
value: typing.Any,
default: typing.Optional[typing.Callable[[typing.Any], typing.Any]],
) -> typing.Any:
"""Return a JSON-ready ``data`` value, leaving a binary payload untouched.

Structured mode emits top-level ``bytes`` as ``data_base64``. Every other
value is passed through the encoder Pydantic supplied to ``json()``
(``json_encoders`` and the ``encoder`` argument), which already accepts
``datetime``, ``date``, ``time``, ``UUID``, ``Decimal``, and nested models.
"""
if value is None or isinstance(value, (bytes, bytearray, memoryview)):
return value
if default is None:
return json.loads(json.dumps(value))
return json.loads(json.dumps(value, default=default))


def _ce_json_dumps( # type: ignore[no-untyped-def]
obj: typing.Dict[str, typing.Any],
*args,
Expand All @@ -47,16 +65,27 @@ def _ce_json_dumps( # type: ignore[no-untyped-def]
Needed by the pydantic base-model to serialize the event correctly to json.
Without this function the data will be incorrectly serialized.

Pydantic applies ``by_alias``, ``exclude_unset``, ``exclude_none``, and
``exclude_defaults`` while building ``obj``. ``args`` and ``kwargs``
(``indent``, ``ensure_ascii``, ``separators``, and the ``default`` encoder)
are forwarded to the final ``json.dumps``. ``data`` is encoded first so
values such as ``datetime`` survive ``conversion.to_json``.

:param obj: CloudEvent represented as a dict.
:param args: User arguments which will be passed to json.dumps function.
:param kwargs: User arguments which will be passed to json.dumps function.

:return: Event serialized as a standard JSON CloudEvent with user specific
parameters.
"""
default = kwargs.get("default")

def marshall(data: typing.Any) -> typing.Any:
return _jsonable_data(data, default)

# Using HTTP from dict due to performance issues.
event = http.from_dict(obj)
event_json = conversion.to_json(event)
event_json = conversion.to_json(event, data_marshaller=marshall)
# Pydantic is known for initialization time lagging.
return json.dumps(
# We SHOULD de-serialize the value, to serialize it back with
Expand Down
59 changes: 56 additions & 3 deletions src/cloudevents/v1/pydantic/v2/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@
from cloudevents.v1.pydantic.fields_docs import FIELD_DESCRIPTIONS

try:
from pydantic import BaseModel, ConfigDict, Field, model_serializer
from pydantic import (
BaseModel,
ConfigDict,
Field,
SerializationInfo,
TypeAdapter,
model_serializer,
)
except ImportError: # pragma: no cover # hard to test
raise PydanticFeatureNotInstalled(
"CloudEvents pydantic feature is not installed. "
Expand All @@ -34,6 +41,39 @@
from cloudevents.v1.exceptions import IncompatibleArgumentsError
from cloudevents.v1.sdk.event import attribute

# JSON encoding for the ``data`` member. The CloudEvent envelope itself is still
# produced by ``conversion.to_json`` so ``data_base64``, omitted null attributes,
# and RFC3339 ``time`` stay on the structured-mode path.
_JSON_DATA: TypeAdapter[Any] = TypeAdapter(Any)


def _jsonable_data(value: Any, info: SerializationInfo) -> Any:
"""Return a JSON-ready ``data`` value, leaving a binary payload untouched.

Structured mode emits top-level ``bytes`` as ``data_base64``. Every other
value is encoded the way ``BaseModel.model_dump_json`` encodes it, including
``datetime``, ``date``, ``time``, ``UUID``, ``Decimal``, and nested models.

Serialization flags from ``model_dump_json`` / ``.json()`` apply to that
payload. ``include`` and ``exclude`` select CloudEvent fields, so they are
not applied inside ``data``.
"""
if value is None or isinstance(value, (bytes, bytearray, memoryview)):
return value
return _JSON_DATA.dump_python(
value,
mode="json",
by_alias=info.by_alias,
exclude_unset=info.exclude_unset,
exclude_defaults=info.exclude_defaults,
exclude_none=info.exclude_none,
exclude_computed_fields=info.exclude_computed_fields,
round_trip=info.round_trip,
serialize_as_any=info.serialize_as_any,
polymorphic_serialization=info.polymorphic_serialization,
context=info.context,
)


class CloudEvent(abstract.CloudEvent, BaseModel): # type: ignore
"""
Expand Down Expand Up @@ -193,22 +233,35 @@ def parse_raw(
return conversion.from_json(cls, b)

@model_serializer(when_used="json")
def _ce_json_dumps(self) -> typing.Dict[str, typing.Any]:
def _ce_json_dumps(self, info: SerializationInfo) -> typing.Dict[str, typing.Any]:
"""Performs Pydantic-specific serialization of the event when
serializing the model using `.model_dump_json()` method.

Needed by the pydantic base-model to serialize the event correctly to json.
Without this function the data will be incorrectly serialized.

``data`` is encoded with Pydantic before the stdlib JSON step inside
``conversion.to_json``, which otherwise rejects values such as
``datetime``. ``info`` carries ``by_alias``, ``exclude_unset``, and the
other ``model_dump_json`` flags through to that payload. ``.json()``
delegates to ``model_dump_json``.

:param self: CloudEvent.
:param info: Serialization options from ``model_dump_json``.

:return: Event serialized as a standard CloudEvent dict with user specific
parameters.
"""

def marshall(value: Any) -> Any:
return _jsonable_data(value, info)

# Here mypy complains about json.loads returning Any
# which is incompatible with this method return type
# but we know it's always a dictionary in this case
return json.loads(conversion.to_json(self)) # type: ignore
return json.loads( # type: ignore
conversion.to_json(self, data_marshaller=marshall)
)

def _get_attributes(self) -> typing.Dict[str, typing.Any]:
return {
Expand Down
Loading
Loading