Skip to content

fix(v1): treat JSON null CloudEvent attributes as omitted - #327

Closed
ekanshbannsal wants to merge 3 commits into
cloudevents:mainfrom
ekanshbannsal:fix/json-null-attributes
Closed

ekanshbannsal wants to merge 3 commits into
cloudevents:mainfrom
ekanshbannsal:fix/json-null-attributes

Conversation

@ekanshbannsal

Copy link
Copy Markdown

Related Issue: #199

Type of change:

  • Bug fix (non-breaking change which fixes an issue)

Description:
Aligns v1.sdk.event.base.BaseEvent JSON serialization and deserialization with the CloudEvents JSON format specification (cloudevents.core.formats.json.JSONFormat.read), treating non-data attributes with value null (None) as omitted:

  • In BaseEvent.UnmarshalJSON, skip attributes where value is None and name not in ("data", "data_base64") instead of setting them as None (which raised AttributeError when marshaling HTTP headers or leaked None into extensions).
  • In BaseEvent.MarshalJSON, omit extension attributes whose value is None.
  • Added test_json_null_attribute_equivalent_to_omitted in tests/test_v1_compat/test_http_conversions.py verifying parity for both 0.3 and 1.0 spec versions.

Pre-submission checklist:

  • I have read the CONTRIBUTING.md file.
  • I have signed off my commits using git commit --signoff.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have run pytest (571 passed), ruff check, and mypy src/cloudevents/v1/sdk/event/base.py.
  • This pull request is ready to be reviewed.

Signed-off-by: Ekansh Bannsal <ekanshbannsal@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Null required attributes bypass validation, allowing an event with "id": null to be accepted and silently assigned a new ID instead of rejected as missing a required field.

1 open issue
What changed in this PR

Updates the v1 compatibility layer to treat null non-data JSON attributes as omitted, addressing the serialization behavior related to #199.

Changes:

  • Skips null non-data attributes during JSON deserialization.
  • Omits extension attributes valued None during JSON serialization.
  • Adds regression coverage for spec versions 0.3 and 1.0, including preservation of null data.
File Description
tests/​test_v1_compat/​test_http_conversions.py Tests null-versus-omitted JSON attribute equivalence for both spec versions.
src/​cloudevents/​v1/​sdk/​event/​base.py Filters null attributes during JSON serialization and deserialization.

Review effort: Balanced

Comment on lines +238 to +239
if value is None and name not in ("data", "data_base64"):
continue
@xSAVIKx

xSAVIKx commented Nov 29, 2026

Copy link
Copy Markdown
Member

I don't think this change should be merged at all. this PR addresses v1 compatibility layer that overall should not be used and is kept in the SDK as a good gesture.
the introduced changes are not required by the spec as mentioned in the related issue.

@xSAVIKx xSAVIKx closed this Nov 29, 2026
@ekanshbannsal
ekanshbannsal deleted the fix/json-null-attributes branch November 29, 2026 18:49
@ekanshbannsal
ekanshbannsal restored the fix/json-null-attributes branch November 29, 2026 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants