在API平台中,我试图装饰服务api_platform.jsonld.normalizer.item
(类ApiPlatform\JsonLd\Serializer\ItemNormalizer
)。
我注意到这在我的应用程序中导致了一些奇怪的行为。例如,我得到一个错误Specified class App\Dto\File\FileMultipleResponseDto is not a resource class.
,它在设置装饰器之前没有显示。
这是我的装饰:
<?php
declare(strict_types=1);
namespace App\Serializer;
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\Serializer\Exception\LogicException;
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;
#[AsDecorator('api_platform.jsonld.normalizer.item')]
class ItemJsonLdNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface
{
public function __construct(
private readonly NormalizerInterface $normalizer,
) {}
public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
{
return $this->normalizer->normalize($object, $format, $context);
}
public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool
{
return $this->normalizer->supportsDenormalization($data, $type, $format, $context);
}
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed
{
return $this->normalizer->denormalize($data, $type, $format, $context);
}
public function hasCacheableSupportsMethod(): bool
{
return $this->normalizer->hasCacheableSupportsMethod();
}
public function supportsNormalization(mixed $data, string $format = null)
{
return $this->normalizer->supportsNormalization($data, $format);
}
public function setSerializer(SerializerInterface $serializer)
{
return $this->normalizer->setSerializer($serializer);
}
}
字符串
正如您所看到的,它目前没有做任何与装饰原始服务无关的事情。我做错了什么?
1条答案
按热度按时间wwwo4jvm1#
所以,经过一些调试(感谢
bin/console debug:container normalizer
命令),我发现了问题:API Platform ElasticSearch组件似乎已经装饰了JsonLD ItemNormalizer(ApiPlatform\Elasticsearch\Serializer\ItemNormalizer
),优先级更高(-895)。它在应用程序中导致了一些问题,我没有进一步调查。我使用了比API平台装饰器更高的优先级:
字符串
现在,对于JsonLD请求,我的规范化器得到了正确的考虑。请注意,您可能还必须为JSON请求实现一个类似的规范化器,因为它们不会进入这个创建的规范化器。
作为参考,我希望在我的代码中解决这个问题:https://github.com/api-platform/api-platform/issues/343