From 33e36f3d2b65b9b6bcfc8e60aabb884e75bef91b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sun, 3 Mar 2024 20:24:02 +0100 Subject: [PATCH] Fixed issue with EntityImported that was caused by the changes to PartNormalizer We now have a possibility to skip API Platforms serializer subsystem --- .../APIPlatform/SkippableItemNormalizer.php | 95 +++++++++++++++++++ .../Attachments/FileTypeFilterTools.php | 5 +- .../ImportExportSystem/EntityImporter.php | 3 + 3 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 src/Serializer/APIPlatform/SkippableItemNormalizer.php diff --git a/src/Serializer/APIPlatform/SkippableItemNormalizer.php b/src/Serializer/APIPlatform/SkippableItemNormalizer.php new file mode 100644 index 00000000..20dc4c01 --- /dev/null +++ b/src/Serializer/APIPlatform/SkippableItemNormalizer.php @@ -0,0 +1,95 @@ +. + */ + +declare(strict_types=1); + + +namespace App\Serializer\APIPlatform; + +use ApiPlatform\Serializer\ItemNormalizer; +use Symfony\Component\DependencyInjection\Attribute\AsDecorator; +use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; +use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; +use Symfony\Component\Serializer\SerializerAwareInterface; +use Symfony\Component\Serializer\SerializerInterface; + +/** + * This class decorates API Platform's ItemNormalizer to allow skipping the normalization process by setting the + * DISABLE_ITEM_NORMALIZER context key to true. This is useful for all kind of serialization operations, where the API + * Platform subsystem should not be used. + */ +#[AsDecorator("api_platform.serializer.normalizer.item")] +class SkippableItemNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface +{ + + public const DISABLE_ITEM_NORMALIZER = 'DISABLE_ITEM_NORMALIZER'; + + public function __construct(private readonly ItemNormalizer $inner) + { + + } + + public function hasCacheableSupportsMethod(): bool + { + return $this->inner->hasCacheableSupportsMethod(); + } + + public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed + { + return $this->inner->denormalize($data, $type, $format, $context); + } + + public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool + { + if ($context[self::DISABLE_ITEM_NORMALIZER] ?? false) { + return false; + } + + return $this->inner->supportsDenormalization($data, $type, $format, $context); + } + + public function normalize(mixed $object, ?string $format = null, array $context = []): float|int|bool|\ArrayObject|array|string|null + { + return $this->inner->normalize($object, $format, $context); + } + + public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool + { + if ($context[self::DISABLE_ITEM_NORMALIZER] ?? false) { + return false; + } + + return $this->inner->supportsNormalization($data, $format, $context); + } + + public function setSerializer(SerializerInterface $serializer): void + { + $this->inner->setSerializer($serializer); + } + + public function getSupportedTypes(?string $format): array + { + //Don't cache results, as we check for the context + return [ + 'object' => false + ]; + } +} \ No newline at end of file diff --git a/src/Services/Attachments/FileTypeFilterTools.php b/src/Services/Attachments/FileTypeFilterTools.php index 0b1b3fc6..d689fda3 100644 --- a/src/Services/Attachments/FileTypeFilterTools.php +++ b/src/Services/Attachments/FileTypeFilterTools.php @@ -119,10 +119,9 @@ class FileTypeFilterTools //Convert jpg to .jpg $element = '.'.$element; } - - //Prevent weird side effects - unset($element); } + //Prevent weird side effects + unset($element); $elements = array_unique($elements); diff --git a/src/Services/ImportExportSystem/EntityImporter.php b/src/Services/ImportExportSystem/EntityImporter.php index 8a4ba0cd..d97171ff 100644 --- a/src/Services/ImportExportSystem/EntityImporter.php +++ b/src/Services/ImportExportSystem/EntityImporter.php @@ -26,6 +26,7 @@ use App\Entity\Base\AbstractNamedDBElement; use App\Entity\Base\AbstractStructuralDBElement; use App\Entity\Parts\Category; use App\Entity\Parts\Part; +use App\Serializer\APIPlatform\SkippableItemNormalizer; use Composer\Semver\Constraint\Constraint; use Symfony\Component\Validator\ConstraintViolationList; use Symplify\EasyCodingStandard\ValueObject\Option; @@ -157,6 +158,8 @@ class EntityImporter 'create_unknown_datastructures' => $options['create_unknown_datastructures'], 'path_delimiter' => $options['path_delimiter'], 'partdb_import' => true, + //Disable API Platform normalizer, as we don't want to use it here + SkippableItemNormalizer::DISABLE_ITEM_NORMALIZER => true, ]); //Ensure we have an array of entity elements.