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
A class like this, that has a property of any type and that is not yet initialized, may cause this error:
Error : Typed property MyEntity::$name must not be accessed before initialization
A bit of context
I'm using api-platform/core to manage my API.
This means that each time a get an entity, it passes through the Serializer and its registered normalizers.
When the normalizer of this library is called, then it is executed and this causes the issue:
Error : Typed property MyClass::$name must not be accessed before initialization /srv/app/src/MyClass.php:149 /srv/app/vendor/prugala/symfony-request-dto/src/Serializer/Normalizer/CustomNormalizer.php:23 /srv/app/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php:390 /srv/app/vendor/symfony/serializer/Serializer.php:234 /srv/app/vendor/symfony/serializer/Serializer.php:152 /srv/app/vendor/api-platform/core/src/Symfony/EventListener/DeserializeListener.php:101 /srv/app/vendor/symfony/event-dispatcher/Debug/WrappedListener.php:116 /srv/app/vendor/symfony/event-dispatcher/EventDispatcher.php:220 /srv/app/vendor/symfony/event-dispatcher/EventDispatcher.php:56 /srv/app/vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php:139 /srv/app/vendor/symfony/http-kernel/HttpKernel.php:158 /srv/app/vendor/symfony/http-kernel/HttpKernel.php:76 /srv/app/vendor/symfony/http-kernel/Kernel.php:197 /srv/app/vendor/symfony/http-kernel/HttpKernelBrowser.php:65 /srv/app/vendor/symfony/framework-bundle/KernelBrowser.php:171 /srv/app/vendor/symfony/browser-kit/AbstractBrowser.php:403 /srv/app/vendor/api-platform/core/src/Symfony/Bundle/Test/Client.php:116 /srv/app/tests/MyClassTest.php:311
How to fix the problem
<?php
namespace Prugala\RequestDto\Serializer\Normalizer;
...
class CustomNormalizer extends ObjectNormalizer
{
...
public function supportsNormalization(mixed $data, string $format = null)
{
return false;
}
+ public function supportsDenormalization(mixed $data,string $type,string $format = null){+ return $data instanceof RequestDtoInterface && parent::supportsDenormalization($data, $type, $format);+ }
}
This change will make the normalizer of this library be executed only on objects that actually implement the required interface RequestDtoInterface and this fixes the problem.
Your environment
Include as many relevant details about the environment you experienced the bug in and how to reproduce it.
PHP version used: 8.1
Symfony version used: 5.4.12
api-platform/core:3.1.14
The text was updated successfully, but these errors were encountered:
Aerendir
added a commit
to Aerendir/symfony-request-dto
that referenced
this issue
Sep 2, 2023
Description
A class like this, that has a property of any type and that is not yet initialized, may cause this error:
A bit of context
I'm using
api-platform/core
to manage my API.This means that each time a get an entity, it passes through the
Serializer
and its registered normalizers.When the normalizer of this library is called, then it is executed and this causes the issue:
How to fix the problem
This change will make the normalizer of this library be executed only on objects that actually implement the required interface
RequestDtoInterface
and this fixes the problem.Your environment
Include as many relevant details about the environment you experienced the bug in and how to reproduce it.
api-platform/core:3.1.14
The text was updated successfully, but these errors were encountered: