Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ApiPlatform deprecations #3674

Merged
merged 4 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/src/Serializer/Denormalizer/InputFilterDenormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\InputFilter\FilterAttribute;
use App\InputFilter\InputFilter;
use App\InputFilter\UnexpectedValueException;
use App\Serializer\NoCachingSupportTrait;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
Expand All @@ -17,6 +18,7 @@
*/
class InputFilterDenormalizer implements DenormalizerInterface, DenormalizerAwareInterface {
use DenormalizerAwareTrait;
use NoCachingSupportTrait;

private const ALREADY_CALLED = 'INPUT_FILTER_DENORMALIZER_ALREADY_CALLED';

Expand Down
2 changes: 2 additions & 0 deletions api/src/Serializer/Denormalizer/MaterialItemDenormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ApiPlatform\Metadata\Post;
use App\Entity\MaterialItem;
use App\Serializer\NoCachingSupportTrait;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
Expand All @@ -15,6 +16,7 @@
*/
class MaterialItemDenormalizer implements DenormalizerInterface, DenormalizerAwareInterface {
use DenormalizerAwareTrait;
use NoCachingSupportTrait;

private const ALREADY_CALLED = 'MATERIAL_ITEM_DENORMALIZER_ALREADY_CALLED';

Expand Down
14 changes: 14 additions & 0 deletions api/src/Serializer/NoCachingSupportTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Serializer;

trait NoCachingSupportTrait {
/**
* We dont want to cache if this type is supported, because it should only be called once.
*
* @see DenormalizerInterface::getSupportedTypes
*/
public function getSupportedTypes(?string $format): array {
return ['*' => false];
}
}
8 changes: 8 additions & 0 deletions api/src/Serializer/Normalizer/CollectionItemsNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public function normalize($object, $format = null, array $context = []): array|s
return $data;
}

public function getSupportedTypes(?string $format): array {
if (method_exists($this->decorated, 'getSupportedTypes')) {
return $this->decorated->getSupportedTypes($format);
}

return ['*' => false];
}

public function setNormalizer(NormalizerInterface $normalizer): void {
if ($this->decorated instanceof NormalizerAwareInterface) {
$this->decorated->setNormalizer($normalizer);
Expand Down
8 changes: 8 additions & 0 deletions api/src/Serializer/Normalizer/ContentTypeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public function normalize($object, $format = null, array $context = []): array|s
return $data;
}

public function getSupportedTypes(?string $format): array {
if (method_exists($this->decorated, 'getSupportedTypes')) {
return $this->decorated->getSupportedTypes($format);
}

return ['*' => false];
}

public function setSerializer(SerializerInterface $serializer): void {
if ($this->decorated instanceof SerializerAwareInterface) {
$this->decorated->setSerializer($serializer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ public function normalize($object, $format = null, array $context = []): array|s
return $data;
}

public function getSupportedTypes(?string $format): array {
if (method_exists($this->decorated, 'getSupportedTypes')) {
return $this->decorated->getSupportedTypes($format);
}

return ['*' => false];
}

public function setSerializer(SerializerInterface $serializer): void {
if ($this->decorated instanceof SerializerAwareInterface) {
$this->decorated->setSerializer($serializer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
use App\Serializer\Normalizer\Error\TranslationInfoOfConstraintViolation;
use App\Service\TranslateToAllLocalesService;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;

class TranslationConstraintViolationListNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface {
class TranslationConstraintViolationListNormalizer implements NormalizerInterface {
public function __construct(
private readonly AbstractConstraintViolationListNormalizer $hydraNormalizer,
private readonly AbstractConstraintViolationListNormalizer $problemNormalizer,
Expand Down Expand Up @@ -72,10 +71,18 @@ public function normalize(mixed $object, string $format = null, array $context =
return $result;
}

public function hasCacheableSupportsMethod(): bool {
return $this->getNormalizerCollection()->forAll(fn ($_, $elem) => $elem->hasCacheableSupportsMethod());
public function getSupportedTypes(?string $format): array {
return $this->getNormalizerCollection()
->map(function (AbstractConstraintViolationListNormalizer $normalizer) use ($format) {
return $normalizer->getSupportedTypes($format);
})
->reduce(fn (array|null $left, array $right) => array_merge($left ?? [], $right), [])
;
}

/**
* @return ArrayCollection<int<0, 1>, AbstractConstraintViolationListNormalizer>
*/
private function getNormalizerCollection(): ArrayCollection {
return new ArrayCollection([$this->hydraNormalizer, $this->problemNormalizer]);
}
Expand Down
11 changes: 5 additions & 6 deletions api/src/Serializer/Normalizer/UriTemplateNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

use ApiPlatform\Api\UrlGeneratorInterface;
use App\Metadata\Resource\Factory\UriTemplateFactory;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\String\Inflector\EnglishInflector;

/**
* This class modifies the API entrypoint when retrieved in HAL JSON format (/index.jsonhal) to include URI templates, and additionally
* makes sure that the relation names of the _links are in plural rather than the default singular of API platform.
*/
class UriTemplateNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface {
class UriTemplateNormalizer implements NormalizerInterface {
public function __construct(
private NormalizerInterface $decorated,
private EnglishInflector $inflector,
Expand Down Expand Up @@ -58,11 +57,11 @@ public function normalize($object, $format = null, array $context = []): array|s
return $result;
}

public function hasCacheableSupportsMethod(): bool {
if (!$this->decorated instanceof CacheableSupportsMethodInterface) {
return false;
public function getSupportedTypes(?string $format): array {
if (method_exists($this->decorated, 'getSupportedTypes')) {
return $this->decorated->getSupportedTypes($format);
}

return $this->decorated->hasCacheableSupportsMethod();
return ['*' => false];
}
}
Loading