You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description
During phpunit tests with APP_DEBUG=1, an anonymous class can be normalized by the NormalizerInterface, however, with APP_DEBUG=0 the following error is produced:
$ APP_DEBUG=1 vendor/bin/phpunit tests/AnonymousClassTest.php
PHPUnit 8.5.4 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 466 ms, Memory: 20.00 MB
OK (1 test, 1 assertion)
Failed test run with APP_DEBUG=0
$ APP_DEBUG=0 vendor/bin/phpunit tests/AnonymousClassTest.php
PHPUnit 8.5.4 by Sebastian Bergmann and contributors.
E 1 / 1 (100%)
Time: 216 ms, Memory: 18.00 MB
There was 1 error:
1) App\Tests\AnonymousClassTest::testSerializeAnonymousClass
Symfony\Component\Cache\Exception\InvalidArgumentException: Cache key "class@anonymous/app/tests/AnonymousClassTest.php:16$9c" contains reserved characters "{}()/\@:".
/app/vendor/symfony/cache/CacheItem.php:177
/app/vendor/symfony/cache/Traits/AbstractTrait.php:281
/app/vendor/symfony/cache/Traits/AbstractAdapterTrait.php:44
/app/vendor/symfony/cache/Adapter/PhpArrayAdapter.php:122
/app/vendor/symfony/serializer/Mapping/Factory/CacheClassMetadataFactory.php:50
/app/vendor/symfony/serializer/Mapping/ClassDiscriminatorFromClassMetadata.php:50
/app/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php:269
/app/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php:164
/app/vendor/symfony/serializer/Serializer.php:152
/app/tests/AnonymousClassTest.php:19
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
Possible Solution
It appears that an issue similar to this has appeared a few times in the past (linked issued below) and it was fixed with something like this in PropertyAccessor::getReadAccessInfo and PropertyAccessor::getWriteAccessInfo:
But this issue seems to be unrelated to the PropertyAccessor, so I adding the change to CacheClassMetadataFactory::getMetadataFor and that makes the test pass successfully with APP_DEBUG=0:
public function getMetadataFor($value)
{
$class = $this->getClass($value);
// Key cannot contain backslashes according to PSR-6
$key = strtr($class, '\\', '_');
+ $key = false !== strpbrk($key, '{}()/@:') ? rawurlencode($key) : $key;
$item = $this->cacheItemPool->getItem($key);
Not sure if this is the correct place to fix the issue, or if it has knock on affects elsewhere.
Symfony version(s) affected: 4.4.7
Description
During phpunit tests with
APP_DEBUG=1
, an anonymous class can be normalized by theNormalizerInterface
, however, withAPP_DEBUG=0
the following error is produced:Full stack trace
How to reproduce
The error can be reproduced with the following test case:
Successful test run with APP_DEBUG=1
Failed test run with APP_DEBUG=0
Possible Solution
It appears that an issue similar to this has appeared a few times in the past (linked issued below) and it was fixed with something like this in
PropertyAccessor::getReadAccessInfo
andPropertyAccessor::getWriteAccessInfo
:But this issue seems to be unrelated to the PropertyAccessor, so I adding the change to
CacheClassMetadataFactory::getMetadataFor
and that makes the test pass successfully withAPP_DEBUG=0
:public function getMetadataFor($value) { $class = $this->getClass($value); // Key cannot contain backslashes according to PSR-6 $key = strtr($class, '\\', '_'); + $key = false !== strpbrk($key, '{}()/@:') ? rawurlencode($key) : $key; $item = $this->cacheItemPool->getItem($key);
Not sure if this is the correct place to fix the issue, or if it has knock on affects elsewhere.
Additional context
I found a few older issues which may be related:
Cache key "p@triggers" contains reserved characters {}()/\@:
#29293The text was updated successfully, but these errors were encountered: