Fixed issue with EntityImported that was caused by the changes to PartNormalizer

We now have a possibility to skip API Platforms serializer subsystem
This commit is contained in:
Jan Böhmer 2024-03-03 20:24:02 +01:00
parent 42e604245c
commit 33e36f3d2b
3 changed files with 100 additions and 3 deletions

View file

@ -0,0 +1,95 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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
];
}
}

View file

@ -119,10 +119,9 @@ class FileTypeFilterTools
//Convert jpg to .jpg //Convert jpg to .jpg
$element = '.'.$element; $element = '.'.$element;
} }
}
//Prevent weird side effects //Prevent weird side effects
unset($element); unset($element);
}
$elements = array_unique($elements); $elements = array_unique($elements);

View file

@ -26,6 +26,7 @@ use App\Entity\Base\AbstractNamedDBElement;
use App\Entity\Base\AbstractStructuralDBElement; use App\Entity\Base\AbstractStructuralDBElement;
use App\Entity\Parts\Category; use App\Entity\Parts\Category;
use App\Entity\Parts\Part; use App\Entity\Parts\Part;
use App\Serializer\APIPlatform\SkippableItemNormalizer;
use Composer\Semver\Constraint\Constraint; use Composer\Semver\Constraint\Constraint;
use Symfony\Component\Validator\ConstraintViolationList; use Symfony\Component\Validator\ConstraintViolationList;
use Symplify\EasyCodingStandard\ValueObject\Option; use Symplify\EasyCodingStandard\ValueObject\Option;
@ -157,6 +158,8 @@ class EntityImporter
'create_unknown_datastructures' => $options['create_unknown_datastructures'], 'create_unknown_datastructures' => $options['create_unknown_datastructures'],
'path_delimiter' => $options['path_delimiter'], 'path_delimiter' => $options['path_delimiter'],
'partdb_import' => true, '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. //Ensure we have an array of entity elements.