Skip to content

Add support Pure Enum and remove IntBackedEnum interface #2088

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
wants to merge 2 commits into from

Conversation

pvlg
Copy link

@pvlg pvlg commented Mar 18, 2023

Removed the IntBackedEnum interface, since it's missing from PHP, the check doesn't make sense.

Added support for Pure Enum.

Added tests for EnumModelDescriber.

@webspec2012
Copy link

Hello everyone Is there any information whether the implementation of this issue is planned?

@GuilhemN
Copy link
Collaborator

Thanks for this PR!

As mentioned in #1965 (comment), I'm still not sure we should support enums that are not BackedEnum though.
Those are not supported by Symfony Serializer as far as I can see, so supporting them would make us have a different behavior.

Unless I'm wrong and pure enums are correctly serialized? What would be your case for pure enums support?

@webspec2012
Copy link

webspec2012 commented Apr 27, 2023

@GuilhemN

Unless I'm wrong and pure enums are correctly serialized? What would be your case for pure enums support?

At the moment I am using DTO (Assert) PHP Enum for validation according to the following recommendation:

https://github.com/symfony/symfony/pull/43047#issuecomment-1161704955

ChannelStatusEnum::class

`
enum ChannelStatusEnum: string
{
case PUBLISHED = 'published';
case UNPUBLISHED = 'unpublished';
case BLOCKED = 'blocked';

public static function values(): array
{
    return \array_column(self::cases(), 'value');
}

}
`

DTO

#[Assert\NotBlank] #[Assert\Choice(callback: [ChannelStatusEnum::class, 'values'] , multiple: false)] public readonly string $status,

The scheme is working, there are no problems. But I can't specify it in the NelmioApiDocBundle.
It doesn't work that way.

Request DTO

#[OA\Property(type: 'string', enum: ChannelStatusEnum::class, example: 'published', nullable: false)] public readonly string $status,

A simple solution would be to specify so:

#[OA\Property(type: 'string', enum: ChannelStatusEnum::values(), nullable: false)] public readonly string $status,

But you can't do that-(

@GuilhemN
Copy link
Collaborator

GuilhemN commented Apr 28, 2023

We automatically extract info from Assert/Choice, see https://github.com/nelmio/NelmioApiDocBundle/blob/master/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php#L107-L109, so just having #[Assert\NotBlank] #[Assert\Choice(callback: [ChannelStatusEnum::class, 'values'] , multiple: false)] public readonly string $status and then #[OA\Property(type: 'string', example: 'published', nullable: false)] public readonly string $status should work.

And a class name in the enum field will not be interpreted by NelmioApiDocBundle also, so I don't think adding support of pure enums would help in your case.

@webspec2012
Copy link

@GuilhemN, Thanks! This solution works.

@DjordyKoert
Copy link
Collaborator

I will go ahead and close this one because of #2121

@DjordyKoert DjordyKoert closed this Jan 2, 2024
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.

4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy