From df23ba07ba2dc1e4d8b625b1f2042e200d7f059d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Fri, 5 Jan 2024 23:38:49 +0100 Subject: [PATCH] Fixed excpetion that no IRI could be generated if a new Part was created via POST operation via API This was because the objectSerializer in PartNormalizer messed up the JSONLD IRI generation of the paramaters property. It tried to generate this IRI via the Part ressource class, which is not possible --- src/Entity/Parts/Part.php | 2 -- src/Entity/Parts/PartTraits/InstockTrait.php | 3 +++ src/Serializer/PartNormalizer.php | 6 +++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Entity/Parts/Part.php b/src/Entity/Parts/Part.php index 88834191..8fa53680 100644 --- a/src/Entity/Parts/Part.php +++ b/src/Entity/Parts/Part.php @@ -104,8 +104,6 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface; #[ApiFilter(RangeFilter::class, properties: ["mass", "minamount"])] #[ApiFilter(DateFilter::class, strategy: DateFilter::EXCLUDE_NULL)] #[ApiFilter(OrderFilter::class, properties: ['name', 'id', 'addedDate', 'lastModified'])] -#[DocumentedAPIProperty(schemaName: 'Part-Read', property: 'total_instock', type: 'number', nullable: false, - description: 'The total amount of this part in stock (sum of all part lots).')] class Part extends AttachmentContainingDBElement { use AdvancedPropertyTrait; diff --git a/src/Entity/Parts/PartTraits/InstockTrait.php b/src/Entity/Parts/PartTraits/InstockTrait.php index fa4b0a22..efa10778 100644 --- a/src/Entity/Parts/PartTraits/InstockTrait.php +++ b/src/Entity/Parts/PartTraits/InstockTrait.php @@ -28,6 +28,7 @@ use App\Entity\Parts\PartLot; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Serializer\Annotation\Groups; +use Symfony\Component\Serializer\Attribute\SerializedName; use Symfony\Component\Validator\Constraints as Assert; /** @@ -181,6 +182,8 @@ trait InstockTrait * * @return float The amount of parts given in partUnit */ + #[Groups(['simple', 'extended', 'full', 'part:read'])] + #[SerializedName('total_instock')] public function getAmountSum(): float { //TODO: Find a method to do this natively in SQL, the current method could be a bit slow diff --git a/src/Serializer/PartNormalizer.php b/src/Serializer/PartNormalizer.php index 509809a7..9767759d 100644 --- a/src/Serializer/PartNormalizer.php +++ b/src/Serializer/PartNormalizer.php @@ -39,6 +39,7 @@ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; /** * @see \App\Tests\Serializer\PartNormalizerTest + * TODO: Properly rewrite this class to use the SerializerAware interface and dont use the ObjectNormalizer directly */ class PartNormalizer implements NormalizerInterface, DenormalizerInterface { @@ -65,7 +66,8 @@ class PartNormalizer implements NormalizerInterface, DenormalizerInterface public function supportsNormalization($data, string $format = null, array $context = []): bool { - return $data instanceof Part; + //We only remove the type field for CSV export + return $format === 'csv' && $data instanceof Part ; } /** @@ -86,8 +88,6 @@ class PartNormalizer implements NormalizerInterface, DenormalizerInterface unset($data['type']); } - $data['total_instock'] = $object->getAmountSum(); - return $data; }