Fixed some serializer deprecations

This commit is contained in:
Jan Böhmer 2023-06-11 18:12:22 +02:00
parent 219b57a362
commit e57d6e508a
7 changed files with 51 additions and 27 deletions

View file

@ -21,13 +21,12 @@
namespace App\Serializer; namespace App\Serializer;
use Brick\Math\BigNumber; use Brick\Math\BigNumber;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/** /**
* @see \App\Tests\Serializer\BigNumberNormalizerTest * @see \App\Tests\Serializer\BigNumberNormalizerTest
*/ */
class BigNumberNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface class BigNumberNormalizer implements NormalizerInterface
{ {
public function supportsNormalization($data, string $format = null, array $context = []): bool public function supportsNormalization($data, string $format = null, array $context = []): bool
@ -44,8 +43,10 @@ class BigNumberNormalizer implements NormalizerInterface, CacheableSupportsMetho
return (string) $object; return (string) $object;
} }
public function hasCacheableSupportsMethod(): bool public function getSupportedTypes(?string $format)
{ {
return true; return [
BigNumber::class => true,
];
} }
} }

View file

@ -27,16 +27,20 @@ use App\Entity\Parts\Supplier;
use App\Entity\PriceInformations\Orderdetail; use App\Entity\PriceInformations\Orderdetail;
use App\Entity\PriceInformations\Pricedetail; use App\Entity\PriceInformations\Pricedetail;
use Brick\Math\BigDecimal; use Brick\Math\BigDecimal;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
/** /**
* @see \App\Tests\Serializer\PartNormalizerTest * @see \App\Tests\Serializer\PartNormalizerTest
*/ */
class PartNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface class PartNormalizer implements NormalizerInterface, DenormalizerInterface
{ {
private const DENORMALIZE_KEY_MAPPING = [ private const DENORMALIZE_KEY_MAPPING = [
'notes' => 'comment', 'notes' => 'comment',
'quantity' => 'instock', 'quantity' => 'instock',
@ -47,7 +51,10 @@ class PartNormalizer implements NormalizerInterface, DenormalizerInterface, Cach
'storage_location' => 'storelocation', 'storage_location' => 'storelocation',
]; ];
public function __construct(private readonly ObjectNormalizer $normalizer, private readonly StructuralElementFromNameDenormalizer $locationDenormalizer) public function __construct(
private readonly StructuralElementFromNameDenormalizer $locationDenormalizer,
#[Autowire(service: ObjectNormalizer::class)]
private readonly NormalizerInterface $normalizer)
{ {
} }
@ -166,9 +173,11 @@ class PartNormalizer implements NormalizerInterface, DenormalizerInterface, Cach
return $object; return $object;
} }
public function hasCacheableSupportsMethod(): bool public function getSupportedTypes(?string $format)
{ {
//Must be false, because we rely on is_array($data) in supportsDenormalization() //Must be false, because we rely on is_array($data) in supportsDenormalization()
return false; return [
Part::class => false,
];
} }
} }

View file

@ -23,19 +23,25 @@ namespace App\Serializer;
use App\Entity\Base\AbstractStructuralDBElement; use App\Entity\Base\AbstractStructuralDBElement;
use App\Repository\StructuralDBElementRepository; use App\Repository\StructuralDBElementRepository;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
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, CacheableSupportsMethodInterface class StructuralElementDenormalizer implements DenormalizerInterface
{ {
private array $object_cache = []; private array $object_cache = [];
public function __construct(private readonly ObjectNormalizer $normalizer, private readonly EntityManagerInterface $entityManager) public function __construct(
private readonly EntityManagerInterface $entityManager,
#[Autowire(service: ObjectNormalizer::class)]
private readonly DenormalizerInterface $denormalizer)
{ {
} }
@ -50,7 +56,7 @@ class StructuralElementDenormalizer implements DenormalizerInterface, CacheableS
public function denormalize($data, string $type, string $format = null, array $context = []): ?AbstractStructuralDBElement public function denormalize($data, string $type, string $format = null, array $context = []): ?AbstractStructuralDBElement
{ {
/** @var AbstractStructuralDBElement $deserialized_entity */ /** @var AbstractStructuralDBElement $deserialized_entity */
$deserialized_entity = $this->normalizer->denormalize($data, $type, $format, $context); $deserialized_entity = $this->denormalizer->denormalize($data, $type, $format, $context);
//Check if we already have the entity in the database (via path) //Check if we already have the entity in the database (via path)
/** @var StructuralDBElementRepository $repo */ /** @var StructuralDBElementRepository $repo */
@ -81,8 +87,11 @@ class StructuralElementDenormalizer implements DenormalizerInterface, CacheableS
return $deserialized_entity; return $deserialized_entity;
} }
public function hasCacheableSupportsMethod(): bool public function getSupportedTypes(): array
{ {
return false; //Must be false, because we use in_array in supportsDenormalization
return [
AbstractStructuralDBElement::class => false,
];
} }
} }

View file

@ -29,7 +29,7 @@ use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
/** /**
* @see \App\Tests\Serializer\StructuralElementFromNameDenormalizerTest * @see \App\Tests\Serializer\StructuralElementFromNameDenormalizerTest
*/ */
class StructuralElementFromNameDenormalizer implements DenormalizerInterface, CacheableSupportsMethodInterface class StructuralElementFromNameDenormalizer implements DenormalizerInterface
{ {
public function __construct(private readonly EntityManagerInterface $em) public function __construct(private readonly EntityManagerInterface $em)
{ {
@ -67,9 +67,11 @@ class StructuralElementFromNameDenormalizer implements DenormalizerInterface, Ca
return end($elements); return end($elements);
} }
public function hasCacheableSupportsMethod(): bool public function getSupportedTypes(?string $format)
{ {
//Must be false, because we do an is_string check on data in supportsDenormalization //Cachable value Must be false, because we do an is_string check on data in supportsDenormalization
return false; return [
AbstractStructuralDBElement::class => false
];
} }
} }

View file

@ -21,16 +21,22 @@
namespace App\Serializer; namespace App\Serializer;
use App\Entity\Base\AbstractStructuralDBElement; use App\Entity\Base\AbstractStructuralDBElement;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
/** /**
* @see \App\Tests\Serializer\StructuralElementNormalizerTest * @see \App\Tests\Serializer\StructuralElementNormalizerTest
*/ */
class StructuralElementNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface class StructuralElementNormalizer implements NormalizerInterface
{ {
public function __construct(private readonly ObjectNormalizer $normalizer) public function __construct(
#[Autowire(service: ObjectNormalizer::class)]
private NormalizerInterface $normalizer
)
{ {
} }
@ -57,8 +63,10 @@ class StructuralElementNormalizer implements NormalizerInterface, CacheableSuppo
return $data; return $data;
} }
public function hasCacheableSupportsMethod(): bool public function getSupportedTypes(?string $format)
{ {
return true; return [
AbstractStructuralDBElement::class => true,
];
} }
} }

View file

@ -30,7 +30,7 @@ class ValidThemeValidator extends ConstraintValidator
{ {
} }
public function validate($value, Constraint $constraint) public function validate($value, Constraint $constraint): void
{ {
if (!$constraint instanceof ValidTheme) { if (!$constraint instanceof ValidTheme) {
throw new UnexpectedTypeException($constraint, ValidTheme::class); throw new UnexpectedTypeException($constraint, ValidTheme::class);

View file

@ -37,11 +37,6 @@ class StructuralElementDenormalizerTest extends WebTestCase
$this->service = self::getContainer()->get(StructuralElementDenormalizer::class); $this->service = self::getContainer()->get(StructuralElementDenormalizer::class);
} }
public function testHasCacheableSupportsMethod(): void
{
$this->assertFalse($this->service->hasCacheableSupportsMethod());
}
public function testSupportsDenormalization(): void public function testSupportsDenormalization(): void
{ {
$this->assertFalse($this->service->supportsDenormalization('doesnt_matter', Category::class, 'json', ['groups' => ['import']])); $this->assertFalse($this->service->supportsDenormalization('doesnt_matter', Category::class, 'json', ['groups' => ['import']]));