8000 [Serializer] Support for PHP 8.1 enums · Issue #40241 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Support for PHP 8.1 enums #40241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ogizanagi opened this issue Feb 18, 2021 · 11 comments · Fixed by #40830
Closed

[Serializer] Support for PHP 8.1 enums #40241

ogizanagi opened this issue Feb 18, 2021 · 11 comments · Fixed by #40830
Labels
Feature Help wanted Issues and PRs which are looking for volunteers to complete them. Serializer

Comments

@ogizanagi
Copy link
Contributor

Description

https://wiki.php.net/rfc/enumerations#backed_enums

Backed enums have scalar equivalents. The Serializer should be able to normalize and denormalize such enums.

Example

class Foo {
    public Suit $suit;
}

$foo = new Foo();
$foo->suit = Suit::Diamonds;
$serialized = $this->serializer->serialize($foo, 'json')
// expected: '{ "suit": "D" }'

$this->serializer->deserialize($serialized, Foo::class);
// expected: Foo->suit === Suit::Diamonds

Should pure enums (no scalar equivalent) be supported as well, by relying on the name property? (Suit::Spades->name)

@ogizanagi ogizanagi added Serializer Feature Help wanted Issues and PRs which are looking for volunteers to complete them. labels Feb 18, 2021
@stof
Copy link
Member
stof commented Feb 18, 2021

IMO, only backed enums should be supported by default.

If you want to use a pure enum while still serializing it, requiring a custom normalizer seems better to me. The name property is specifically meant as a debugging tool in the Enum RFC. Exposing it in your API looks wrong to me.

@stof
Copy link
Member
stof commented Feb 18, 2021

regarding the implementation, this should be done by building a BackedEnumNormalizer

@Neirda24
Copy link
Contributor

But enums can also implement Interfaces and expose public functions maybe in the ObjectNormalizer ?

@ogizanagi
Copy link
Contributor Author
ogizanagi commented Feb 18, 2021

@Neirda24 : Hmm. 🤔 We might leverage #39399 to choose between serializing the enum as its simple scalar equivalent, or whether to serialize it as an object with serialized methods.
To me, the scalar normalization by BackedEnumNormalizer should be the default, but could be opt-out with the context to let the ObjectNormalizer do its job.

@stof
Copy link
Member
stof commented Feb 18, 2021

@Neirda24 the BackedEnumNormalizer normalizer should be registered with a low priority, so that registering a custom normalizer can override the normalization easily.

@Neirda24
Copy link
Contributor

@stof : sure. I was talking about the public methods that can automagically be serialized when serializing an object. I agree that enums should be kept simple objects. But this could be a use case as the gap between enums and objects is quite thin. Just making sure to explictly says that BackedEnum is not treated as other objects and will by default only dump the current value.

@stof
Copy link
Member
stof commented Feb 18, 2021

Only one normalizer will run (the first supporting the type)

@rzeczkowskip
Copy link

I might try to write the normalizer if You choose me ;)

I think that public methods shouldn't be serialized automatically. It's custom normalizer's job.

@derrabus
Copy link
Member

I guess we need to wait for php/php-src#6489 to be merged first. :-)

@rzeczkowskip
Copy link

Yes. I'm just can't wait for enums and offer my help asap

@iluuu1994
Copy link
Contributor

I guess we need to wait for php/php-src#6489 to be merged first. :-)

Done!

@fabpot fabpot closed this as completed Jul 5, 2021
fabpot added a commit that referenced this issue Jul 5, 2021
…lexandre-daubois)

This PR was merged into the 5.4 branch.

Discussion
----------

[Serializer] Add support of PHP backed enumerations

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #40241 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License       | MIT
| Doc PR        | _none_

![Capture d’écran du 2021-04-15 21-41-51](https://user-images.githubusercontent.com/2144837/114930179-fe7c9e80-9e34-11eb-9a63-3268f8c62307.png)

Commits
-------

3458e0e [Serializer] Add support of PHP backed enumerations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Help wanted Issues and PRs which are looking for volunteers to complete them. Serializer
Projects
None yet
7 participants
0