2

正如文档所建议的那样,我编写了此服务以将 uuid 添加到规范化对象:

<?php
namespace App\Serializer;

use InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\SerializerAwareInterface;
use Symfony\Component\Serializer\SerializerInterface;

final class ApiNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
{
    private DenormalizerInterface|NormalizerInterface $decorated;

    public function __construct(NormalizerInterface $decorated)
    {
        if (!$decorated instanceof DenormalizerInterface) {
            throw new InvalidArgumentException(sprintf('The decorated normalizer must implement the %s.', DenormalizerInterface::class));
        }

        $this->decorated = $decorated;
    }

    public function supportsNormalization($data, $format = null)
    {
        return $this->decorated->supportsNormalization($data, $format);
    }

    public function normalize($object, $format = null, array $context = [])
    {
        $data = $this->decorated->normalize($object, $format, $context);
        if(method_exists($object, 'getUuid') && is_array($data)){
            $data['uuid'] = $object->getUuid();
        }
        return $data;
    }

    /**
     * @param mixed $data
     * @param string $type
     * @param null $format
     *
     * @return bool
     */
    public function supportsDenormalization($data, $type, $format = null)
    {
        return $this->decorated->supportsDenormalization($data, $type, $format);
    }

    public function denormalize($data, $type, $format = null, array $context = [])
    {
        return $this->decorated->denormalize($data, $type, $format, $context);
    }

    public function setSerializer(SerializerInterface $serializer)
    {
        if($this->decorated instanceof SerializerAwareInterface) {
            $this->decorated->setSerializer($serializer);
        }
    }
}

services.yaml

App\Serializer\ApiNormalizer:
    # By default .inner is passed as argument
    decorates: 'api_platform.jsonld.normalizer.item'

但是突然(直到昨天一切都很好)当我运行作曲家或缓存清除时它开始给我这个错误

在 CheckCircularReferencesPass.php 第 67 行:检测到服务“App\Serializer\ApiNormalizer”的循环引用,路径:“App\Serializer\ApiNormalizer -> 序列化程序 -> App\Serializer\ApiNormalizer”。

4

1 回答 1

3

这是版本 5.3.7 上 Symfony 依赖注入组件的问题。

https://github.com/symfony/symfony/issues/42792

这很可能会在明天或后天得到解决。

同时,只需将其添加到您的composer.json

"conflict": {
    "symfony/dependency-injection": "5.3.7"
},

通过这种方式,您可以保持其余依赖项的更新,并且它只会排除 DI 5.3.7 以支持 5.3.6。

当 5.3.8 发布时,您将能够直接更新。

于 2021-08-31T15:05:16.743 回答