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

Determine eloquent attribute type from casts #345

Conversation

kburton-dev
Copy link
Contributor

As mentioned in this discussion, the schema types differ slightly when generating the OpenAPI json, if using different databases, such as MySQL and SQLite.

This is to be expected if the idea is to rely on the DB schema alone to generate the API schema from Eloquent models.

This PR aims to add support for the various model casts that Laravel provides.

@@ -89,15 +86,53 @@ private function getBaseAttributeType(Model $model, string $key, array $value)
default => new UnknownType("unimplemented DB column type [$type[0]]"),
};

if ($value['cast'] && function_exists('enum_exists') && enum_exists($value['cast'])) {
if (! isset($value['cast']::cases()[0]->value)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this check is for backed enums, but might not be necessary as Laravel supports both.

@@ -89,15 +86,53 @@ private function getBaseAttributeType(Model $model, string $key, array $value)
default => new UnknownType("unimplemented DB column type [$type[0]]"),
};

if ($value['cast'] && function_exists('enum_exists') && enum_exists($value['cast'])) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

With PHP 8.1 being the min version now, function_exists('enum_exists') check is not required anymore

@romalytvynenko
Copy link
Member

@kburton-dev what a great work!

Thank you so much for your time, I appreciate it!

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.

2 participants