-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Fix unitialized properties (from PHP 7.4.2) when serializing context for the cache key #36332
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
[Serializer] Fix unitialized properties (from PHP 7.4.2) when serializing context for the cache key #36332
Conversation
To be honest, I'm not sure if what you describe in the description is a bug or not, because when using typed properties, you must always initialize the property, either in its definition of in the constructor, that's the spirit of this feature. However, your patch is legit, because according to the Doctrine documentation:
Could you add a test to prevent regressions? (Just testing than putting something to serializable such as a closure in |
Also, there is probably something to fix in Doctrine too. Our call to |
It cannot really tested since serializing a closure will throw an exception, not an error (and the exception is catched). |
Pay attention to the destination branch: symfony:3.4 here, but the initial issue #35574 is about symfony:4.4. This should be applied on several versions? I reproduce this behavior on SF 4.4 here: doctrine/common#886 (comment) |
@alanpoulain for the test you can do the like it that: https://github.com/symfony/symfony/pull/36336/files#diff-e6bd6c603753788029968840196a888cR395 @2ec0b4 patches are merged to upper branches, it will be fixed in 3.4 and all superior versions. |
…zing context for the cache key
03bd90a
to
1fafff7
Compare
Thank you @alanpoulain. |
This bug only happens on the following conditions:
Book
) having a relation with another entity (Author
) is used;Author
entity uses typed properties (PHP 7.4) not initialized;Serializer
is used with theBook
in theOBJECT_TO_POPULATE
key in the context.For instance:
Or even:
If the following is done (it's the case for instance in API Platform when a
PUT
is made):Then there will be the following error:
It's because of these lines in the
getCacheKey
method of theAbstractObjectNormalizer
:symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
Lines 405 to 409 in 5da141b
Since the lazy proxyfied relation has a
__sleep
with unitialized properties, theserialize
method will throw (since https://bugs.php.net/bug.php?id=79002: php/php-src@846b647).I propose to fix this issue by unsetting the
OBJECT_TO_POPULATE
key in the context because I don't think it's useful for determining the attributes of the object.For the next versions of Symfony, the fix should probably be elsewhere, in the default context.
For instance in Symfony 4.4, instead of:
symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
Line 118 in 15edfd3
It should be:
But I'm not sure how it should be merged (another PR maybe?).