Skip to content

[Serializer] Fix denormalizing custom class in UidNormalizer #44600

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

Merged
merged 1 commit into from
Dec 15, 2021

Conversation

fancyweb
Copy link
Contributor

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

Considering we support denormalizing any subclass of AbstractUid::class we should not denormalize any $type !== Ulid::class to Uuid::class because this is wrong. We need to call $type::fromString (see TestCustomUid).
But fromString() is an abstract method, so it cannot be called directly on abstract classes, I guess we should have not supported them from the start. However, since they work right now (since all abstract classes fall back to the Uuid:class behavior) we cannot break it. So I propose to deprecate supporting abstract classes in 6.1 and remove it in 7.0.

@fancyweb fancyweb force-pushed the serializer/fix-custom-uid branch from 0634cf2 to ba9e002 Compare December 13, 2021 16:53
} catch (\InvalidArgumentException $exception) {
throw new NotNormalizableValueException(sprintf('The data is not a valid "%s" string representation.', $type));
} catch (\Error $e) {
if (str_starts_with($e->getMessage(), 'Cannot instantiate abstract class')) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Catching the error and looking for the message looks better than doing reflection to me, maybe there's a better way?

@nicolas-grekas
Copy link
Member

Thank you @fancyweb.

@nicolas-grekas nicolas-grekas merged commit fdc61b4 into symfony:5.3 Dec 15, 2021
@fancyweb fancyweb deleted the serializer/fix-custom-uid branch December 15, 2021 11:01
nicolas-grekas added a commit that referenced this pull request Dec 15, 2021
This PR was merged into the 5.3 branch.

Discussion
----------

[Serializer] Fix symfony/uid requirement

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

#44600 needs 8247779

Commits
-------

6a0fd54 [Serializer] Fix symfony/uid requirement
This was referenced Dec 29, 2021
nicolas-grekas added a commit that referenced this pull request Feb 23, 2022
…alization in UidNormalizer (fancyweb)

This PR was merged into the 6.1 branch.

Discussion
----------

[Serializer] Deprecate support for abstract uid denormalization in UidNormalizer

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

Continuation of #44600. All abstract classes should not be denormalized to Uuid::class. We need a concrete class. Instead of penalizing everyone with reflection in supportsDenormalization(), let's just bubble up the PHP \Error (we are going to do the same in UidValueResolver).

Commits
-------

6fc4287 [Serializer] Deprecate support for abstract uid denormalization in UidNormalizer
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.

3 participants