Skip to content

feat(core): add JSON Batch format support - #299

Closed
PlugaruT wants to merge 2 commits into
cloudevents:mainfrom
PlugaruT:feat/json-batch-core
Closed

PlugaruT wants to merge 2 commits into
cloudevents:mainfrom
PlugaruT:feat/json-batch-core

Conversation

@PlugaruT

Copy link
Copy Markdown
Member

What

Adds support for the CloudEvents JSON Batch format (application/cloudevents-batch+json) to the cloudevents.core package, across the JSON format layer and the HTTP binding.

Batch mode is only defined by the spec for the HTTP protocol binding (via the JSON Batch format); Kafka and AMQP define only structured/binary modes, so this change is intentionally scoped to JSON + HTTP.

Changes

  • formats/json.py: refactor write/read onto shared _event_to_dict/_dict_to_event helpers (no behavior change), then add BATCH_CONTENT_TYPE, write_batch, read_batch, and get_batch_content_type.
  • formats/base.py: extend the Format protocol with write_batch/read_batch/get_batch_content_type so any format implements the same contract.
  • bindings/http.py: add to_batch/from_batch plus the to_batch_event/from_batch_event JSON-default wrappers; from_http now rejects batch payloads with a clear, case-insensitive error pointing callers to from_batch.

Behavior

  • Empty array ↔ empty list.
  • Per-element version auto-detection — a batch may mix v0.3 and v1.0 events.
  • Binary data round-trips (data_base64 for v1.0, datacontentencoding: base64 for v0.3).
  • A malformed/invalid element aborts the whole call (no partial results).
  • A non-array body raises ValueError.

Tests

Added coverage for round-trip, empty batch, mixed-version batches, data_base64 elements, invalid-element abort, non-array body, and from_http batch rejection (incl. charset param and mixed-case content type). Full tests/test_core suite passes (356 tests); ruff clean.

Implement the CloudEvents JSON Batch format
(application/cloudevents-batch+json) in the core package.

- formats/json.py: extract shared _event_to_dict/_dict_to_event helpers
  and add BATCH_CONTENT_TYPE, write_batch, read_batch, get_batch_content_type
- formats/base.py: extend the Format protocol with the batch methods
- bindings/http.py: add to_batch/from_batch and to_batch_event/from_batch_event,
  and reject batch payloads in from_http with a clear error
- tests: cover round-trip, empty batch, mixed 0.3/1.0 versions, data_base64,
  invalid-element abort, non-array body, and from_http batch rejection

Signed-off-by: PlugaruT <plugaru.tudor@protonmail.com>
@PlugaruT
PlugaruT marked this pull request as ready for review June 27, 2026 14:44
@PlugaruT
PlugaruT requested a review from a team June 27, 2026 14:45

@xSAVIKx xSAVIKx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@PlugaruT, please take a look at some comments and suggestions noted below. IMO the biggest part here is to split the Format/BatchFormat to ease future development.

Comment thread src/cloudevents/core/bindings/http.py Resolved
Comment thread src/cloudevents/core/bindings/http.py Resolved
Comment thread src/cloudevents/core/bindings/http.py Resolved
Comment thread src/cloudevents/core/formats/base.py Resolved
Comment thread src/cloudevents/core/formats/json.py Resolved
Comment thread src/cloudevents/core/formats/json.py Resolved
Comment thread src/cloudevents/core/formats/json.py Resolved
- Split batch methods (write_batch/read_batch/get_batch_content_type)
  out of the Format protocol into a separate, runtime-checkable
  BatchFormat protocol so non-batch formats need not implement batching
  and existing Format implementations keep working.
- Rename HTTP convenience wrappers to_batch_event/from_batch_event to
  to_events_batch/from_events_batch (a batch of events, not a batch event).
- from_http: guard batch detection behind isinstance(BatchFormat),
  compare content types case-insensitively on both sides, and report the
  received vs. expected content type in the error.
- read_batch: include the actual JSON type in the non-array error, reject
  non-object elements, and wrap per-element failures with the element
  index for a clearer error.

Signed-off-by: PlugaruT <plugaru.tudor@protonmail.com>
@PlugaruT
PlugaruT force-pushed the feat/json-batch-core branch from 04fc5ed to dc60518 Compare July 8, 2026 20:37
@PlugaruT
PlugaruT requested a review from xSAVIKx July 9, 2026 05:52
Comment on lines +339 to +345
content_type = ""
for key, value in message.headers.items():
if key.lower() == CONTENT_TYPE_HEADER:
content_type = value
break

received_content_type = content_type.split(";")[0].strip().lower()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this content_type and received_content_type lines are only relevant if we're processing BatchFormat. Let's move them under the check or better have a private named method.

paths cannot drift.
"""
event_data = event.get_data()
event_dict: dict[str, Any] = dict(event.get_attributes())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
event_dict: dict[str, Any] = dict(event.get_attributes())
event_attributes: dict[str, Any] = dict(event.get_attributes())

event_dict: dict[str, Any] = dict(event.get_attributes())
specversion = event_dict.get("specversion", SPECVERSION_V1_0)

if event_data is not None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why don't we invert the condition? This way you'll have a fast way out with return event_dict and then just continue processing the branches. this will allow us to get rid of the nesting

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

Batch writers reject normally typed event lists due to invariant list annotations, and HTTP batch helpers lack required validation that all events share the same specversion.

2 open issues
What changed in this PR

Adds JSON Batch support to cloudevents.core and its HTTP binding, while keeping batching optional for other formats.

Changes:

  • Adds shared JSON conversion helpers and batch serialization/deserialization.
  • Introduces a separate BatchFormat protocol, HTTP batch helpers, and JSON-default wrappers.
  • Adds batch tests covering round trips, version detection, invalid input, and rejection by single-event HTTP parsing.
File Description
tests/​test_core/​test_format/​test_json.py Tests JSON batch encoding, decoding, and invalid elements.
tests/​test_core/​test_bindings/​test_http.py Tests HTTP batch helpers and single-event batch rejection.
src/​cloudevents/​core/​formats/​json.py Implements JSON batch support using shared event conversion helpers.
src/​cloudevents/​core/​formats/​base.py Defines the optional, runtime-checkable BatchFormat protocol.
src/​cloudevents/​core/​bindings/​http.py Adds batch APIs and batch-content detection in from_http.

Review effort: Balanced

:return: HTTPMessage with the batch in the body
"""
headers = {CONTENT_TYPE_HEADER: event_format.get_batch_content_type()}
body = event_format.write_batch(events)
(e.g. ``from_http``) can detect batch capability with ``isinstance``.
"""

def write_batch(self, events: list[BaseCloudEvent]) -> bytes:
@duglin

duglin commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

@PlugaruT still interested in this? Otherwise we'll close it

@PlugaruT

Copy link
Copy Markdown
Member Author

@PlugaruT still interested in this? Otherwise we'll close it

I am but no time on my end. I'll close and reopen later in case I come back to it

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