mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 09:35:49 +02:00
Fixed problem that attachment API responses, did not contain an @id and @context attribute
This commit is contained in:
parent
aad62f2599
commit
c8cde94ee3
1 changed files with 17 additions and 5 deletions
|
@ -26,18 +26,22 @@ namespace App\Serializer;
|
||||||
use App\Entity\Attachments\Attachment;
|
use App\Entity\Attachments\Attachment;
|
||||||
use App\Services\Attachments\AttachmentURLGenerator;
|
use App\Services\Attachments\AttachmentURLGenerator;
|
||||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||||
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
|
||||||
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
|
||||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||||
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
||||||
|
|
||||||
class AttachmentNormalizer implements NormalizerInterface
|
class AttachmentNormalizer implements NormalizerInterface, NormalizerAwareInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
|
use NormalizerAwareTrait;
|
||||||
|
|
||||||
|
private const ALREADY_CALLED = 'ATTACHMENT_NORMALIZER_ALREADY_CALLED';
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
#[Autowire(service: ObjectNormalizer::class)]
|
|
||||||
private readonly NormalizerInterface $normalizer,
|
|
||||||
private readonly AttachmentURLGenerator $attachmentURLGenerator,
|
private readonly AttachmentURLGenerator $attachmentURLGenerator,
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function normalize(mixed $object, string $format = null, array $context = []): array|null
|
public function normalize(mixed $object, string $format = null, array $context = []): array|null
|
||||||
|
@ -46,6 +50,9 @@ class AttachmentNormalizer implements NormalizerInterface
|
||||||
throw new \InvalidArgumentException('This normalizer only supports Attachment objects!');
|
throw new \InvalidArgumentException('This normalizer only supports Attachment objects!');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Prevent loops, by adding a flag to the context
|
||||||
|
$context[self::ALREADY_CALLED] = true;
|
||||||
|
|
||||||
$data = $this->normalizer->normalize($object, $format, $context);
|
$data = $this->normalizer->normalize($object, $format, $context);
|
||||||
|
|
||||||
$data['media_url'] = $this->attachmentURLGenerator->getViewURL($object);
|
$data['media_url'] = $this->attachmentURLGenerator->getViewURL($object);
|
||||||
|
@ -57,6 +64,11 @@ class AttachmentNormalizer implements NormalizerInterface
|
||||||
|
|
||||||
public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
|
public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
|
||||||
{
|
{
|
||||||
|
// avoid recursion: only call once per object
|
||||||
|
if (isset($context[self::ALREADY_CALLED])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return $data instanceof Attachment;
|
return $data instanceof Attachment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +76,7 @@ class AttachmentNormalizer implements NormalizerInterface
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
//We depend on the context to determine if we should normalize or not
|
//We depend on the context to determine if we should normalize or not
|
||||||
Attachment::class => true,
|
Attachment::class => false,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue