Do not inject ObjectNormalizer into StructuralElementDenormalizer directly

This commit is contained in:
Jan Böhmer 2024-06-21 23:41:52 +02:00
parent 43a68b96ae
commit 6821e668e4
2 changed files with 14 additions and 4 deletions

View file

@ -24,23 +24,26 @@ namespace App\Serializer;
use App\Entity\Base\AbstractStructuralDBElement; use App\Entity\Base\AbstractStructuralDBElement;
use App\Repository\StructuralDBElementRepository; use App\Repository\StructuralDBElementRepository;
use App\Serializer\APIPlatform\SkippableItemNormalizer;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
/** /**
* @see \App\Tests\Serializer\StructuralElementDenormalizerTest * @see \App\Tests\Serializer\StructuralElementDenormalizerTest
*/ */
class StructuralElementDenormalizer implements DenormalizerInterface class StructuralElementDenormalizer implements DenormalizerInterface, DenormalizerAwareInterface
{ {
use DenormalizerAwareTrait;
private array $object_cache = []; private array $object_cache = [];
public function __construct( public function __construct(
private readonly EntityManagerInterface $entityManager, private readonly EntityManagerInterface $entityManager)
#[Autowire(service: ObjectNormalizer::class)]
private readonly DenormalizerInterface $denormalizer)
{ {
} }
@ -59,6 +62,9 @@ class StructuralElementDenormalizer implements DenormalizerInterface
public function denormalize($data, string $type, string $format = null, array $context = []): ?AbstractStructuralDBElement public function denormalize($data, string $type, string $format = null, array $context = []): ?AbstractStructuralDBElement
{ {
//Do not use API Platform's denormalizer
$context[SkippableItemNormalizer::DISABLE_ITEM_NORMALIZER] = true;
/** @var AbstractStructuralDBElement $deserialized_entity */ /** @var AbstractStructuralDBElement $deserialized_entity */
$deserialized_entity = $this->denormalizer->denormalize($data, $type, $format, $context); $deserialized_entity = $this->denormalizer->denormalize($data, $type, $format, $context);

View file

@ -37,6 +37,10 @@ class StructuralElementDenormalizerTest extends WebTestCase
//Get a service instance. //Get a service instance.
self::bootKernel(); self::bootKernel();
$this->service = self::getContainer()->get(StructuralElementDenormalizer::class); $this->service = self::getContainer()->get(StructuralElementDenormalizer::class);
//We need to inject the serializer into the normalizer, as we use it directly
$serializer = self::getContainer()->get('serializer');
$this->service->setDenormalizer($serializer);
} }
public function testSupportsDenormalization(): void public function testSupportsDenormalization(): void