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

Enum field mapping validation #11037

Closed
yceruto opened this issue Nov 2, 2023 · 5 comments · Fixed by #11039
Closed

Enum field mapping validation #11037

yceruto opened this issue Nov 2, 2023 · 5 comments · Fixed by #11039
Labels

Comments

@yceruto
Copy link
Contributor

yceruto commented Nov 2, 2023

The recently properties types validation implemented in #10946 is not working properly for enum mappings:

$ bin/console doctrine:schema:validate

Mapping

[FAIL] The entity-class App\Model\Product mapping is invalid:

  • The field 'App\Model\Product#status' has the property type 'App\Model\ProductStatus' that differs from the metadata field type 'string' returned by the 'string' DBAL type.

// If the property type is the same as the metadata field type, we are ok
if ($propertyType === $metadataFieldType) {
return null;
}

where $propertyType is a BackedEnum class and $metadataFieldType is string or int.

Possible solution

I'd add another check (just below that block) to validate the enum mapping, something like this worked for me:

if (
    is_a($propertyType, BackedEnum::class, true)
    && isset($fieldMapping['enumType'])
    && $propertyType === $fieldMapping['enumType']
    && $metadataFieldType === get_debug_type($propertyType::cases()[0]->value)
) {
    return null;
}

It checks the property type against the enumType instead, but also checks if the BackedEnum variant (string or int) matches the metadata field type.

Does it make sense to you?

Affected versions: 2.17.x+

@derrabus
Copy link
Member

derrabus commented Nov 2, 2023

Can you provide a reproducer or a failing test for your issue? If you've already found a solution (as you apparently did), don't hesitate to open a PR with your fix and a test that fails without your fix.

@yceruto
Copy link
Contributor Author

yceruto commented Nov 3, 2023

Here we go #11039

@W0rma
Copy link
Contributor

W0rma commented Nov 16, 2023

I stumbled over this issue when using doctrine/orm:2.17.

Seems like 2.17 was released before this issue has been fixed.

@delolmo
Copy link

delolmo commented Nov 16, 2023

Having the same problem here when updating to 2.17. When trying to validate the mappings, all enumTypes started failing. They didn't on previous versions.

@stlrnz
Copy link

stlrnz commented Nov 16, 2023

I can confirm this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants