Skip to content

fix: Serialize native PHP enums with name and value instead of opaque 'Object' string#2038

Open
jarstelfox wants to merge 3 commits intogetsentry:masterfrom
jarstelfox:fix/enum-serialization
Open

fix: Serialize native PHP enums with name and value instead of opaque 'Object' string#2038
jarstelfox wants to merge 3 commits intogetsentry:masterfrom
jarstelfox:fix/enum-serialization

Conversation

@jarstelfox
Copy link
Contributor

Summary

  • Fixes native PHP enums (8.1+) being serialized as Object ClassName instead of showing their actual enum case and value
  • Adds early-return for UnitEnum in serializeRecursively() before the generic object block, so enums are handled correctly regardless of serializeAllObjects setting
  • Enhances BackedEnum output to include the backing value: Enum ClassName::CASE(value)

Fixes #2037

Test plan

  • Added testBackedEnumsIncludeValue — verifies backed enum output includes the value
  • Added testEnumsAreNotSerializedAsObjects — regression test with serializeAllObjects enabled/disabled
  • Added testBackedEnumsAreNotSerializedAsObjects — same for backed enums
  • All 177 serializer tests pass with no regressions

🤖 Generated with Claude Code

jarstelfox and others added 2 commits March 25, 2026 15:59
…opaque 'Object' string

Enums were caught by the generic `is_object` branch in `serializeRecursively()`,
causing them to render as `Object ClassName` when `serializeAllObjects` was enabled.
This adds an early-return for `UnitEnum` before the object block and enhances the
output to include the backing value for `BackedEnum` instances.

Fixes getsentry#2037

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Copy link
Collaborator

@stayallive stayallive left a comment

Choose a reason for hiding this comment

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

Overal this looks good, thanks for taking the time.

One change is that we would like to not potentially clobber any custom serializers by moving the if statement down a bit. Can you make this change?

Comment on lines +119 to +121
if ($value instanceof \UnitEnum) {
return $this->serializeValue($value);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should move this down to where we check for the DateTimeInterface. This way you can still provide your own serializer if you want to and only if there is none we shall default to serializing it ourselves (which is a sensible change in itself).

…erializer priority

Moves the enum early-return inside the is_object block, after the class
serializers loop, so custom serializers registered via class_serializers
still take precedence over the default enum serialization.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Copy link
Contributor Author

@jarstelfox jarstelfox left a comment

Choose a reason for hiding this comment

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

Good call, moved the UnitEnum check inside the is_object block after the class serializers loop so custom serializers still take precedence. Updated and pushed!

@jarstelfox jarstelfox requested a review from stayallive March 26, 2026 16:59
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.

Native PHP enums serialize as opaque 'Object <ClassName>' in stack traces

2 participants