Skip to content

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

Open
jarstelfox wants to merge 4 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 4 commits intogetsentry:masterfrom
jarstelfox:fix/enum-serialization

Conversation

@jarstelfox
Copy link
Copy Markdown
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) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
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?

…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) <noreply@anthropic.com>
Copy link
Copy Markdown
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
@stayallive stayallive requested a review from Litarnus March 27, 2026 15:26
Copy link
Copy Markdown
Contributor

@Litarnus Litarnus left a comment

Choose a reason for hiding this comment

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

Looks good overall, thank you! Small nit: we are only testing the output format for backed enums, maybe we would be useful to add a regular enum test too

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

$result = $this->invokeSerialization($serializer, $input);

$this->assertSame('Enum Sentry\Tests\Serializer\SerializerTestEnum::CASE_NAME', $result);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

New test duplicates existing test method exactly

Low Severity

testUnitEnumsShowName is an exact duplicate of the pre-existing testEnumsAreNames — identical serializer setup, identical input (SerializerTestEnum::CASE_NAME), and identical assertion. This adds no additional test coverage.

Fix in Cursor Fix in Web

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

3 participants