PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 10, 2022

[FIXED] Circular reference problem with decorated service

 January 10, 2022     api-platform.com, php, symfony, symfony-dependency-injection     No comments   

Issue

As the documentation suggests I wrote this service to add the uuid to the normalized object:

<?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);
        }
    }
}

And services.yaml

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

But suddenly (until yesterday everything was fine) it starts to give me this error when I run composer or cache clear

In CheckCircularReferencesPass.php line 67: Circular reference detected for service "App\Serializer\ApiNormalizer", path: "App\Serializer\ApiNormalizer -> serializer -> App\Serializer\ApiNormalizer".


Solution

This is an issue with Symfony Dependency Injection component on version 5.3.7.

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

This is likely going to be resolved tomorrow or the day after.

On the meantime, just add this to your composer.json:

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

This way you can keep the rest of the dependencies updated, and it will just exclude DI 5.3.7 in favour of 5.3.6.

When 5.3.8 is released, you'll be able to update directly.



Answered By - yivi
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing