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

[DoctrineBridge] Extend type guessing on enum fields #46676

Merged
merged 1 commit into from Jun 19, 2022
Merged

[DoctrineBridge] Extend type guessing on enum fields #46676

merged 1 commit into from Jun 19, 2022

Conversation

Sajito
Copy link

@Sajito Sajito commented Jun 15, 2022

Q A
Branch? 4.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets -
License MIT
Doc PR -

Doctrine supports enumType on array values, while the current implementation always assumes the value to be an enum object, if enumType is set.

If $typeOfField is json_array the collectionKeyType will still be null, while collectionValueType will be filled. That's because we have no way to determine from doctrine metadata, if the key is string or int. Yet I don't know if that's a valid thing to do, otherwise we should return null in that case.

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 6.2 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@carsonbot
Copy link

It looks like you unchecked the "Allow edits from maintainer" box. That is fine, but please note that if you have multiple commits, you'll need to squash your commits into one before this can be merged. Or, you can check the "Allow edits from maintainers" box and the maintainer can squash for you.

Cheers!

Carsonbot

@dunglas
Copy link
Member

dunglas commented Jun 15, 2022

I would indeed return null instead of an incomplete type.

Doctrine supports enumType on array values. In those cases the guessed type
should be of type array with collection information.
@Sajito
Copy link
Author

Sajito commented Jun 16, 2022

Returning null now instead of a type without collectionKeyType.

@norkunas
Copy link
Contributor

I also use just json field to declare array of enums, could we support that?

@Sajito
Copy link
Author

Sajito commented Jun 16, 2022

Actually that's the reason for this PR. There is no way the DoctrineExtractor can guess the correct type from doctrine json field metadata. But that's not a problem, since getPhpType does not handle json fields, therefore will already return null for those cases. So another extractor will handle those cases.

@nicolas-grekas
Copy link
Member

Thank you @Sajito.

nicolas-grekas added a commit that referenced this pull request Jun 19, 2022
…no Chianese)

This PR was merged into the 4.4 branch.

Discussion
----------

[DoctrineBridge] Extend type guessing on enum fields

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Doctrine supports enumType on array values, while the current implementation always assumes the value to be an enum object, if enumType is set.

If $typeOfField is `json_array` the collectionKeyType will still be null, while collectionValueType will be filled. That's because we have no way to determine from doctrine metadata, if the key is string or int. Yet I don't know if that's a valid thing to do, otherwise we should return null in that case.

Commits
-------

79239fe CS fix
a9b0f43 [DoctrineBridge] Extend type guessing on enum fields
@nicolas-grekas nicolas-grekas merged commit a9b0f43 into symfony:4.4 Jun 19, 2022
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))];
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT), $enumType ?? new Type(Type::BUILTIN_TYPE_STRING))];
}
case Type::BUILTIN_TYPE_INT:
Copy link
Member

Choose a reason for hiding this comment

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

I may have merged to fast here.
@Sajito is the missing break before this line on purpose? Can you please send a PR that adds either the "break" or the "no break" comment, with a test case ideally?

Copy link
Author

Choose a reason for hiding this comment

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

I have sent a new PR here: #46713.
I didn't know a "no break" comment would be required here, as I took these lines as a reference:

switch ($typeOfField) {
case self::$useDeprecatedConstants ? DBALType::DATE : Types::DATE_MUTABLE:
case self::$useDeprecatedConstants ? DBALType::DATETIME : Types::DATETIME_MUTABLE:
case self::$useDeprecatedConstants ? DBALType::DATETIMETZ : Types::DATETIMETZ_MUTABLE:
case 'vardatetime':
case self::$useDeprecatedConstants ? DBALType::TIME : Types::TIME_MUTABLE:
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTime')];

Hope it's fine with the new PR.

nicolas-grekas added a commit that referenced this pull request Jun 19, 2022
This PR was merged into the 4.4 branch.

Discussion
----------

[DoctrineBridge] Add missing break

~~This PR only adds the "no break" comment as requested on my last PR:~~
This PR only adds the missing break as requested on my last PR: #46676 (comment)

I hope it's fine to not include the describing table. ~~The missing break is on purpose, so here is no real code change.~~ The missing break should not have caused any side effects.

I have not changed any tests here, because both cases are already tested by these two lines:
https://github.com/symfony/symfony/blob/7fc7acb1e4448762f438af03cadb2ce54e3918e0/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php#L186-L187

Commits
-------

445b78a [DoctrineBridge] Add missing break
@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jun 20, 2022

It looks like I failed at merging this PR into branch 5.4. There, the added test fails. Could you please have a look @Sajito?

Here is the failure on branch 5.4:

1) Symfony\Bridge\Doctrine\Tests\PropertyInfo\DoctrineExtractorTest::testExtractEnum
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
         'collectionKeyType' => Array (...)
         'collectionValueType' => Array (
             0 => Symfony\Component\PropertyInfo\Type Object (
-                'builtinType' => 'object'
+                'builtinType' => 'string'
                 'nullable' => false
-                'class' => 'Symfony\Bridge\Doctrine\Tests...numInt'
+                'class' => null
                 'collection' => false
                 'collectionKeyType' => Array ()
                 'collectionValueType' => Array ()

~/Code/symfony/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php:142

@Sajito
Copy link
Author

Sajito commented Jun 20, 2022

I created another pull request to fix the issue on the 5.4 branch.

nicolas-grekas added a commit that referenced this pull request Jun 21, 2022
… Chianese)

This PR was merged into the 5.4 branch.

Discussion
----------

[DoctrineBridge] Fix value type for simple_array

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        |

Fix for wrong collection value type on `simple_array` after merging the type guessing changes (#46676) into 5.4 branch.

Commits
-------

6375990 [DoctrineBridge] Fix value type for simple_array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants