Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[var-exporter][doctrine] Serialization error with Ignore attribute #54501

Closed as not planned
@sungvadan

Description

@sungvadan

Symfony version(s) affected

6.4.6

Description

I encounter a serialization error when I update to version 6.4.6
"Ignore on \u0022Proxies\\__CG__\\App\\Entity\\Claim::__setInitialized()\u0022 cannot be added. Ignore can only be added on methods beginning with \u0022get\u0022, \u0022is\u0022, \u0022has\u0022 or \u0022set\u0022."

How to reproduce

The problem comes from adding the [#Ignore] attribute in vendor/symfony/var-exporter/LazyGhostTrait.php :

# vendor/symfony/var-exporter/LazyGhostTrait.php

    #[Ignore]
    private function setLazyObjectAsInitialized(bool $initialized): void
    {
        $state = $this->lazyObjectState ?? null;

        if ($state && !\is_array($state->initializer)) {
            $state->status = $initialized ? LazyObjectState::STATUS_INITIALIZED_FULL : LazyObjectState::STATUS_UNINITIALIZED_FULL;
        }
    }

ProxyFactory of doctrine use the trait but override the method name in vendor/doctrine/orm/src/Proxy/ProxyFactory.php line 574

#vendor/doctrine/orm/src/Proxy/ProxyFactory.php: 574

    private function generateUseLazyGhostTrait(ClassMetadata $class): string
    {
        $code = ProxyHelper::generateLazyGhost($class->getReflectionClass());
        $code = substr($code, 7 + (int) strpos($code, "\n{"));
        $code = substr($code, 0, (int) strpos($code, "\n}"));
        $code = str_replace('LazyGhostTrait;', str_replace("\n    ", "\n", 'LazyGhostTrait {
            initializeLazyObject as __load;
            setLazyObjectAsInitialized as public __setInitialized;
            isLazyObjectInitialized as private;
            createLazyGhost as private;
            resetLazyObject as private;
        }'), $code);

        return $code;
    }

And Then when doctrine generate a proxy I have something like

class Claim extends \App\Entity\Claim implements \Doctrine\ORM\Proxy\InternalProxy
{
    use \Symfony\Component\VarExporter\LazyGhostTrait {
        initializeLazyObject as __load;
        setLazyObjectAsInitialized as public __setInitialized;
        isLazyObjectInitialized as private;
        createLazyGhost as private;
        resetLazyObject as private;
    }

Then in vendor/symfony/serializer/Mapping/Loader/AttributeLoader.php line 131 and 172: the method __setLazyObjectAsInitialized no longer satisfies the condition of accessorOrMutator and an MappingException is thrown

# vendor/symfony/serializer/Mapping/Loader/AttributeLoader.php

$accessorOrMutator = preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches);

elseif ($annotation instanceof Ignore) {
       if (!$accessorOrMutator) {
             throw new MappingException(sprintf('Ignore on "%s::%s()" cannot be added. Ignore can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
       }

       $attributeMetadata->setIgnore(true);
}

Possible Solution

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions