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

Error : Typed property MyEntity::$name must not be accessed before initialization #10

Closed
Aerendir opened this issue Sep 2, 2023 · 0 comments · Fixed by #11
Closed

Comments

@Aerendir
Copy link
Contributor

Aerendir commented Sep 2, 2023

Description

class MyClass {
    private string $name;
}

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
Aerendir added a commit to Aerendir/symfony-request-dto that referenced this issue Sep 2, 2023
Aerendir added a commit to Aerendir/symfony-request-dto that referenced this issue Sep 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant