fix: Serialize native PHP enums with name and value instead of opaque 'Object' string#2038
fix: Serialize native PHP enums with name and value instead of opaque 'Object' string#2038jarstelfox wants to merge 4 commits intogetsentry:masterfrom
Conversation
…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>
stayallive
left a comment
There was a problem hiding this comment.
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>
jarstelfox
left a comment
There was a problem hiding this comment.
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!
Litarnus
left a comment
There was a problem hiding this comment.
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>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
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); | ||
| } |
There was a problem hiding this comment.


Summary
Object ClassNameinstead of showing their actual enum case and valueUnitEnuminserializeRecursively()before the generic object block, so enums are handled correctly regardless ofserializeAllObjectssettingBackedEnumoutput to include the backing value:Enum ClassName::CASE(value)Fixes #2037
Test plan
testBackedEnumsIncludeValue— verifies backed enum output includes the valuetestEnumsAreNotSerializedAsObjects— regression test withserializeAllObjectsenabled/disabledtestBackedEnumsAreNotSerializedAsObjects— same for backed enums🤖 Generated with Claude Code