Skip to content
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

Fix enum backing type always 'string' #2121

Merged
merged 9 commits into from
Jan 2, 2024
Merged

Conversation

DidierLmn
Copy link
Contributor

Every enum will be described as type "string" because the current logic to determine the backing type does not work

is_subclass_of($enumClass, \StringBackedEnum::class));
is_subclass_of($enumClass, \IntBackedEnum::class));

These are always false, regardless of backing type as you can see at https://3v4l.org/fButY

Moreover the type would be set as "int", which is not valid in OpenApi-Spec.

This MR ensures proper backing detection of enums and sets int-backed enums to type "integer".

$schema->type = 'integer';
} else {
$schema->type = 'string';
}
$schema->type = is_subclass_of($enumClass, \IntBackedEnum::class) ? 'int' : 'string';
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not really a fan of the if-else combined with the multi places where $schema->type gets set.

Suggested change
$schema->type = is_subclass_of($enumClass, \IntBackedEnum::class) ? 'int' : 'string';
$schema->type = is_subclass_of($enumClass, \IntBackedEnum::class) || ($reflectionEnum->isBacked() && 'int' === $reflectionEnum->getBackingType()->getName())
? 'integer'
: 'string';

@DjordyKoert
Copy link
Collaborator

Also please rebase on master for CI

@DjordyKoert DjordyKoert merged commit 9ff1d64 into nelmio:master Jan 2, 2024
12 checks passed
@DjordyKoert
Copy link
Collaborator

Thank you @DidierLmn!

@DidierLmn
Copy link
Contributor Author

I kinda lost track of this one. Thanks a lot for finishing it off and merging @DjordyKoert!! 🙏

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.

None yet

3 participants