Skip to content
Closed
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
7 changes: 5 additions & 2 deletions src/cloudevents/v1/sdk/event/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def MarshalJSON(
props["data"] = data
if "extensions" in props:
extensions = props.pop("extensions")
props.update(extensions)
props.update({k: v for k, v in extensions.items() if v is not None})
return json.dumps(props)

def UnmarshalJSON(
Expand All @@ -228,13 +228,16 @@ def UnmarshalJSON(
) -> None:
raw_ce = json.loads(b)

missing_fields = self._ce_required_fields - raw_ce.keys()
non_null_keys = {k for k, v in raw_ce.items() if v is not None}
missing_fields = self._ce_required_fields - non_null_keys
if len(missing_fields) > 0:
raise cloud_exceptions.MissingRequiredFields(
f"Missing required attributes: {missing_fields}"
)

for name, value in raw_ce.items():
if value is None and name not in ("data", "data_base64"):
continue
Comment on lines +239 to +240
try:
if name == "data":
decoded_value = data_unmarshaller(json.dumps(value))
Expand Down
39 changes: 39 additions & 0 deletions tests/test_v1_compat/test_http_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import pytest

import cloudevents.v1.exceptions as cloud_exceptions
from cloudevents.v1.conversion import to_dict, to_json
from cloudevents.v1.http import CloudEvent, from_dict, from_json
from cloudevents.v1.sdk.event.attribute import SpecVersion
Expand Down Expand Up @@ -132,6 +133,44 @@ def test_json_can_talk_to_itself_base64(specversion):
assert event.data == data


@pytest.mark.parametrize("specversion", ["0.3", "1.0"])
def test_json_null_attribute_equivalent_to_omitted(specversion):
base_attrs = {
"specversion": specversion,
"id": "1234",
"source": "https://example.com/event-producer",
"type": "com.example.string",
"time": "2023-01-01T00:00:00Z",
}
event_with_none = CloudEvent({**base_attrs, "extnone": None, "subject": None})
event_without = CloudEvent(base_attrs)

assert json.loads(to_json(event_with_none)) == json.loads(to_json(event_without))
assert from_json(to_json(event_with_none)) == from_json(to_json(event_without))

raw_with_null = json.dumps({**base_attrs, "extnone": None, "subject": None})
assert from_json(raw_with_null) == from_json(to_json(event_without))

raw_with_null_data = json.dumps({**base_attrs, "extnone": None, "data": None})
parsed_null_data = from_json(raw_with_null_data)
assert parsed_null_data.data is None
assert "extnone" not in parsed_null_data


@pytest.mark.parametrize("specversion", ["0.3", "1.0"])
@pytest.mark.parametrize("required_field", ["id", "source", "type", "specversion"])
def test_json_null_required_attribute_raises(specversion, required_field):
attrs = {
"specversion": specversion,
"id": "1234",
"source": "https://example.com/event-producer",
"type": "com.example.string",
required_field: None,
}
with pytest.raises(cloud_exceptions.MissingRequiredFields):
from_json(json.dumps(attrs))


def test_from_dict():
given = {
"data": b"\x00\x00\x11Hello World",
Expand Down