mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 09:35:49 +02:00
Implemented basic discriminator map for Attachment API access. Now we can properly create part attachments using a POST operation
This commit is contained in:
parent
fad8f26eaa
commit
16b3d58242
3 changed files with 73 additions and 1 deletions
|
@ -43,6 +43,7 @@ use App\Validator\Constraints\Selectable;
|
|||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Serializer\Annotation\Groups;
|
||||
use Symfony\Component\Serializer\Annotation\SerializedName;
|
||||
use Symfony\Component\Serializer\Attribute\DiscriminatorMap;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
use function in_array;
|
||||
use InvalidArgumentException;
|
||||
|
@ -72,7 +73,7 @@ use LogicException;
|
|||
operations: [
|
||||
new Get(security: 'is_granted("read", object)'),
|
||||
new GetCollection(security: 'is_granted("@attachments.list_attachments")'),
|
||||
//new Post(securityPostDenormalize: 'is_granted("create", object)'),
|
||||
new Post(securityPostDenormalize: 'is_granted("create", object)'),
|
||||
new Patch(security: 'is_granted("edit", object)'),
|
||||
new Delete(security: 'is_granted("delete", object)'),
|
||||
],
|
||||
|
@ -88,6 +89,7 @@ use LogicException;
|
|||
#[ApiFilter(EntityFilter::class, properties: ["attachment_type"])]
|
||||
#[ApiFilter(DateFilter::class, strategy: DateFilter::EXCLUDE_NULL)]
|
||||
#[ApiFilter(OrderFilter::class, properties: ['name', 'id', 'addedDate', 'lastModified'])]
|
||||
#[DiscriminatorMap(typeProperty: '_type', mapping: ['part' => PartAttachment::class])]
|
||||
abstract class Attachment extends AbstractNamedDBElement
|
||||
{
|
||||
/**
|
||||
|
@ -143,6 +145,7 @@ abstract class Attachment extends AbstractNamedDBElement
|
|||
* @phpstan-param T|null $element
|
||||
*/
|
||||
#[Groups(['attachment:read:standalone', 'attachment:write:standalone'])]
|
||||
#[ApiProperty(writableLink: false)]
|
||||
protected ?AttachmentContainingDBElement $element = null;
|
||||
|
||||
#[ORM\Column(type: Types::BOOLEAN)]
|
||||
|
|
|
@ -23,8 +23,14 @@ declare(strict_types=1);
|
|||
namespace App\Entity\Attachments;
|
||||
|
||||
use App\Entity\Parts\Part;
|
||||
use App\Serializer\OverrideClassDenormalizer;
|
||||
use App\Serializer\TypeOverride\TypeOverridableSerializer;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
||||
use Symfony\Component\PropertyInfo\Type;
|
||||
use Symfony\Component\Serializer\Attribute\Context;
|
||||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
|
||||
|
||||
/**
|
||||
* A attachment attached to a part element.
|
||||
|
@ -40,5 +46,7 @@ class PartAttachment extends Attachment
|
|||
*/
|
||||
#[ORM\ManyToOne(targetEntity: Part::class, inversedBy: 'attachments')]
|
||||
#[ORM\JoinColumn(name: 'element_id', nullable: false, onDelete: 'CASCADE')]
|
||||
// Set the correct type for the element property to use
|
||||
#[Context(denormalizationContext: [OverrideClassDenormalizer::CONTEXT_KEY => Part::class])]
|
||||
protected ?AttachmentContainingDBElement $element = null;
|
||||
}
|
||||
|
|
61
src/Serializer/OverrideClassDenormalizer.php
Normal file
61
src/Serializer/OverrideClassDenormalizer.php
Normal file
|
@ -0,0 +1,61 @@
|
|||
<?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;
|
||||
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
|
||||
/**
|
||||
* The idea of this denormalizer is to allow to override the type of the object created using a certain context key.
|
||||
* This is required to resolve the issue of the serializer/API platform not correctly being able to determine the type
|
||||
* of the "element" properties of the Attachment and Parameter subclasses.
|
||||
*/
|
||||
class OverrideClassDenormalizer implements DenormalizerInterface, DenormalizerAwareInterface
|
||||
{
|
||||
use DenormalizerAwareTrait;
|
||||
|
||||
public const CONTEXT_KEY = '__override_type__';
|
||||
|
||||
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = [])
|
||||
{
|
||||
//Deserialize the data with the overridden type
|
||||
$overrideType = $context[self::CONTEXT_KEY];
|
||||
unset($context[self::CONTEXT_KEY]);
|
||||
|
||||
return $this->denormalizer->denormalize($data, $overrideType, $format, $context);
|
||||
}
|
||||
|
||||
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
|
||||
{
|
||||
return isset($context[self::CONTEXT_KEY]);
|
||||
}
|
||||
|
||||
public function getSupportedTypes(?string $format): array
|
||||
{
|
||||
return [
|
||||
'*' => false,
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue