Skip to content

feat: default datacontenttype to application/json - #14

Merged
Meldiron merged 1 commit into
mainfrom
feat-datacontenttype-default
Jul 31, 2026
Merged

Meldiron merged 1 commit into
mainfrom
feat-datacontenttype-default

Conversation

@Meldiron

Copy link
Copy Markdown
Contributor

What

The constructor now defaults datacontenttype to application/json instead of leaving it unset. The parameter stays nullable, so datacontenttype: null still leaves the attribute unset (and omitted from toArray()).

$event = new CloudEvent(
    type: 'com.example.user.created',
    source: 'https://example.com/user-service',
    id: uniqid(),
    data: ['userId' => '123'],
);

$event->datacontenttype; // 'application/json'

Why

JSON payloads are the overwhelmingly common case, and every caller was spelling out datacontenttype: 'application/json' by hand.

Notes

  • fromArray() deliberately keeps preserving the wire form — an absent datacontenttype stays absent on the parsed event (guarded by the existing testFromArrayDoesNotFabricateDatacontenttype). Per the JSON format spec, an absent datacontenttype already implies a JSON payload, so nothing needs to be fabricated when parsing.
  • toArray() on a default-constructed event now includes 'datacontenttype' => 'application/json', which is a behavior change for producers relying on the attribute being omitted.

🤖 Generated with Claude Code

@greptile-apps

greptile-apps Bot commented Jul 30, 2026 •

Copy link
Copy Markdown

Greptile Summary

The PR changes the constructor’s default datacontenttype while preserving explicit-null and wire-parsing behavior.

  • Defaults constructor-created events to application/json.
  • Keeps datacontenttype: null available to omit the attribute.
  • Documents the new default and updates its test coverage.

Confidence Score: 5/5

This PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
src/CloudEvents/CloudEvent.php Changes the constructor default to application/json while ensuring fromArray() continues to preserve an absent wire attribute.
tests/CloudEvents/CloudEventTest.php Updates default serialization expectations and tests the explicit-null behavior.
README.md Documents the constructor default and the explicit-null escape hatch.

Reviews (4): Last reviewed commit: "feat: default datacontenttype to applica..." | Re-trigger Greptile

@Meldiron
Meldiron force-pushed the feat-datacontenttype-default branch from 1cdafe1 to fbff67c Compare July 30, 2026 11:06
@Meldiron
Meldiron force-pushed the docs-spec-accuracy branch from eb1675c to aed7a6b Compare July 31, 2026 10:00
@Meldiron
Meldiron changed the base branch from docs-spec-accuracy to main July 31, 2026 10:01
@Meldiron
Meldiron force-pushed the feat-datacontenttype-default branch from fbff67c to 00eb1c9 Compare July 31, 2026 10:03
@Meldiron
Meldiron changed the base branch from main to docs-spec-accuracy July 31, 2026 10:06
@Meldiron
Meldiron changed the base branch from docs-spec-accuracy to main July 31, 2026 10:09
The constructor now defaults datacontenttype to "application/json",
the overwhelmingly common case, instead of leaving it unset. The
parameter stays nullable so callers can pass null to leave the
attribute unset.

fromArray() deliberately keeps preserving the wire form: an absent
datacontenttype stays absent, since the JSON format already treats an
absent datacontenttype as implying a JSON payload.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@Meldiron
Meldiron force-pushed the feat-datacontenttype-default branch from 00eb1c9 to 8168ffd Compare July 31, 2026 10:10
@Meldiron
Meldiron merged commit 4f5a541 into main Jul 31, 2026
4 checks passed
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.

1 participant