Skip to content

fix: use an empty body for binary events without data - #68

Open
EmiRZx wants to merge 1 commit into
cloudevents:mainfrom
EmiRZx:fix-binary-no-data
Open

EmiRZx wants to merge 1 commit into
cloudevents:mainfrom
EmiRZx:fix-binary-no-data

Conversation

@EmiRZx

@EmiRZx EmiRZx commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

fixes #67. a binary event without data now gets an empty body. an empty binary body unmarshals to an event without data, so the SDK reads back what it sends. new tests cover the serializer, the deserializer and an HTTP round trip. they fail on main and pass here. tests pass on PHP 7.4 and 8.4. phpcs and psalm are clean.

note that the body on the wire changes from null to empty. receivers on 1.2.0 and older reject an empty body. they should be upgraded before senders.

Marshalling a binary event without data read a missing array key. That
raised an 'Undefined array key "data"' warning and wrote the text null
as the body, even for non-JSON content types. The body is now empty.

The unmarshaller reads an empty binary body as an event without data
instead of failing with InvalidPayloadSyntaxException, so the SDK can
read back what it sends.

Signed-off-by: emir <52245396+EmiRZx@users.noreply.github.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.

🟢 Approval recommended

No active review issues remain for the change to handle events without data using empty binary bodies.

No open issues 🎉

What changed in this PR

Fixes #67 by using an empty binary body for events without data and accepting empty bodies when deserializing.

Changes:

  • Avoids accessing a missing data key during serialization.
  • Interprets an empty binary body as no event data.
  • Adds serializer, deserializer, and HTTP round-trip regression tests.

Older receivers must be upgraded before senders adopt the new wire format.

File Description
tests/​Unit/​Serializers/​JsonSerializerTest.php Tests empty-body serialization.
tests/​Unit/​Serializers/​JsonDeserializerTest.php Tests empty-body deserialization.
tests/​Unit/​Http/​MarshallerTest.php Tests an HTTP round trip without data.
src/​Serializers/​JsonSerializer.php Emits an empty body when data is absent.
src/​Serializers/​JsonDeserializer.php Accepts an empty body as no data.

Review effort: Balanced

@duglin

duglin commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

@EmiRZx for an existing app, this will switch from getting an error to return an empty body, right?

@EmiRZx

EmiRZx commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

mostly yes. on main sending raises a PHP warning, not an exception. it's Undefined array key "data", a notice on 7.4. the body is the four bytes null. it only becomes an error when the app turns warnings into exceptions, as Laravel or Symfony in debug mode do. with this PR there's no warning and the body is empty.

receiving changes too. an empty body used to throw InvalidPayloadSyntaxException and now gives an event without data. a null body still gives no data. events from older senders keep working. the other direction doesn't. an older receiver throws on the new empty body. I still think the empty body is right. in binary mode the body is the data and this event has none. tested on PHP 7.4 and 8.4.

@duglin

duglin commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

mostly yes. on main sending raises a PHP warning, not an exception. it's Undefined array key "data", a notice on 7.4. the body is the four bytes null. it only becomes an error when the app turns warnings into exceptions, as Laravel or Symfony in debug mode do. with this PR there's no warning and the body is empty.

receiving changes too. an empty body used to throw InvalidPayloadSyntaxException and now gives an event without data. a null body still gives no data. events from older senders keep working. the other direction doesn't. an older receiver throws on the new empty body. I still think the empty body is right. in binary mode the body is the data and this event has none. tested on PHP 7.4 and 8.4.

That scares me a little.
@ jlaswell @GrahamCampbell any thoughts?

@EmiRZx

EmiRZx commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

this stayed on my mind too and I looked into it a bit more. Go, JavaScript, Python, Java, Rust and C# all send an empty body for an event without data. so released sdk-php receivers already throw on events without data from any other SDK. most of them also leave out Content-Type when there's no datacontenttype. sdk-php rejects that too and #73 covers it. the null body works between PHP apps. Go reads it as the bytes null and JavaScript as the string "null".

a safer way is two steps. this PR could keep sending null without the warning and only teach receivers to accept an empty body. nothing on the wire changes then and no mix of versions breaks. the switch to an empty body can wait for a later release or a 2.0. happy to update the PR that way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Binary marshalling of an event without data warns and writes null

3 participants