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

[Serializer] Undefined property: Proxies\__CG__\App\Entity\ChildEntity::$lazyObjectState #54393

Closed
alexandre-le-borgne opened this issue Mar 25, 2024 · 3 comments

Comments

@alexandre-le-borgne
Copy link

alexandre-le-borgne commented Mar 25, 2024

Symfony version(s) affected

7.0.4 & 6.4.4

Description

When serializing a doctrine proxy of an entity and using the ignored attribute on a property of the entity, I get the error :

User Notice: Undefined property: Proxies_CG_\App\Entity\ChildEntity::$lazyObjectState
in /home/../vendor/symfony/property-access/PropertyAccessor.php on line 408

Removing the property with the #[Ignore] attribute fixes the problem.

How to reproduce

https://github.com/alexandre-le-borgne/bug-symfony-phpdoc-serialization

Diff: alexandre-le-borgne/bug-symfony-phpdoc-serialization@07b80b9

// IndexController.php
#[AsController]
class IndexController {
  public function __construct(private SerializerInterface $serializer, private EntityManagerInterface $doctrine)
  {
  }
    
  #[Route('/')]
  public function __invoke()
    $parent = $this->doctrine->find(ParentEntity::class, 1);
    $model = new ExampleModel();
    // Trying to serialize an object containing a proxy from doctrine
    $model->childEntity = $parent->getChildEntity();
    dd($this->serializer->normalize($model));
  }
}
#[Entity]
class ParentEntity
{
    #[Id]
    #[Column]
    #[GeneratedValue]
    private int $id;

    #[OneToOne(cascade: ['persist'])]
    private ChildEntity $childEntity;


    public function getId(): int
    {
        return $this->id;
    }

    public function getChildEntity(): ChildEntity
    {
        return $this->childEntity;
    }

    public function setChildEntity(ChildEntity $childEntity): void
    {
        $this->childEntity = $childEntity;
    }
}
#[Entity]
class ChildEntity
{
    #[Id]
    #[Column]
    #[GeneratedValue]
    private int $id;

    #[Ignore]
    public string $ignored = 'ignored';

    public function getId(): int
    {
        return $this->id;
    }
}
class ExampleModel
{
    /**
     * @var ChildEntity
     */
    public ChildEntity $childEntity;
}
class ChildEntity
{
    #[Id]
    #[Column]
    #[GeneratedValue]
    private int $id;

    #[Ignore]
    public string $ignored = 'ignored';

    public function getId(): int
    {
        return $this->id;
    }
}

Removing the property "$ignored" with the Ignore attribute in the ChildEntity fixes the problem.

Possible Solution

Add in config/framework.yaml

framework:
    serializer:
        default_context:
            ignored_attributes:
                - '__initializer__' 
                - '__cloner__'
                - '__isInitialized__'
                - 'lazyObjectState'
                - 'lazyObjectInitialized'
                - 'lazyObjectAsInitialized'

Additional Context

Seems related to the code in AbstractNormalizer.php

        $ignoreUsed = false;
        // ...

        if (!$ignoreUsed && [] === $groups && $allowExtraAttributes) {
            // Backward Compatibility with the code using this method written before the introduction of @Ignore
            return false;
        }
        return $allowedAttributes;

image

@stof
Copy link
Member

stof commented Mar 25, 2024

Can you try again with the dev version of Symfony ? #54194 is not included in the 7.0.4 and 6.4.4 releases as it has been merged after them.

@alexandre-le-borgne
Copy link
Author

alexandre-le-borgne commented Mar 25, 2024

@stof Done.

I ran the command composer require symfony/property-access:dev -W
As expected (#54194 (comment)), the error is still there

@xabbuh
Copy link
Member

xabbuh commented Apr 5, 2024

You also need to update symfony/var-exporter and symfony/serializer to have the fixes from #54224 and #54485 (will be part of the next patch releases). I have tested your example application (thanks for it, that helped a lot 👍) and it works (again) with the patches from the linked PRs.

@xabbuh xabbuh closed this as completed Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants