受到 Ryan Weaver 关于 SymfonyCast 上的 ApiPlatform 的精彩教程的启发,我创建了一个 Normalizer 和 Denormalizer 来管理组标准化和非标准化。
GroupsDenormalizer
装饰
api_platform.serializer.normalizer.item
GroupsNormalizer
装饰
api_platform.jsonld.normalizer.item
我使用
#[AsDecorator]
属性来做到这一点。一切正常!我的测试覆盖了 100% 的代码。没有弃用。 一旦我从 ApiPlatform 3.1.20 升级到 3.2.0,我的代码就会失败,并且 Api 不再可用。
TypeError: ApiPlatform\Symfony\Validator\Serializer\ValidationExceptionNormalizer::__construct(): 参数 #1 ($decorated) 必须是 Symfony\Component\Serializer\Normalizer\NormalizerInterface、App\Security\GroupsDenormalizer 类型,在 /srv 中调用/app/var/cache/test/ContainerCco4qUx/App_KernelTestDebugContainer.php 第 2484 行我不明白为什么依赖注入将我的反规范化器作为 ValidationExceptionNormalizer 构造函数的参数发送。
我试图找到有关从 3.1 迁移到 3.2 的文档(没有成功)。 我尝试更新 api-platform 的配方。 我尝试改变优先级。 我尝试在
services.yaml
中定义装饰。一旦删除
GroupsDenormalizer.php
文件,Api 就再次可用。 (但是我的代码失败了,因为我的反规范化器有工作要做:))我错过了什么?你有想法吗?
下面,您可以找到这两个类的代码。
#[AsDecorator(decorates: 'api_platform.jsonld.normalizer.item', priority: 64)]
readonly class GroupsNormalizer implements NormalizerInterface, SerializerAwareInterface
{
public function __construct(
private NormalizerInterface $decorated,
private GroupsGenerator $generator,
) {
}
/**
* @return array<string, bool>
*/
public function getSupportedTypes(?string $format): array
{
return [
Character::class => true,
User::class => true,
];
}
public function normalize(mixed $object, string $format = null, array $context = []): float|int|bool|\ArrayObject|array|string|null
{
if (true /* $object instanceof ResourceInterface */) {
$complement = $this->generator->generateReadingGroups($object);
if (!key_exists('groups', $context)) {
$context['groups'] = [];
}
$context['groups'] = array_merge($context['groups'], $complement);
}
return $this->decorated->normalize($object, $format, $context);
}
public function setSerializer(SerializerInterface $serializer): void
{
if ($this->decorated instanceof SerializerAwareInterface) {
$this->decorated->setSerializer($serializer);
}
}
public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
{
return $this->decorated->supportsNormalization($data, $format);
}
}
#[AsDecorator(decorates: 'api_platform.serializer.normalizer.item', priority: 64)]
readonly class GroupsDenormalizer implements DenormalizerInterface, SerializerAwareInterface
{
public function __construct(
private DenormalizerInterface $decorated,
private GroupsGenerator $generator,
) {
}
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed
{
if (key_exists('operation', $context) && $context['operation'] instanceof Operation) {
$complement = $this->generator->generateWritingGroups($context['operation'], $type);
if (!key_exists('groups', $context)) {
$context['groups'] = [];
}
$context['groups'] = array_merge($context['groups'], $complement);
}
return $this->decorated->denormalize($data, $type, $format, $context);
}
/**
* @return array<string, bool>
*/
public function getSupportedTypes(?string $format): array
{
return [
Character::class => true,
User::class => true,
];
}
public function setSerializer(SerializerInterface $serializer): void
{
if ($this->decorated instanceof SerializerAwareInterface) {
$this->decorated->setSerializer($serializer);
}
}
public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool
{
return $this->decorated->supportsDenormalization($data, $type, $format, $context);
}
}
/**
* @return array<string, bool>
*/
public function getSupportedTypes(?string $format): array
{
return [
Character::class => false,
User::class => false,
'*' => null,
];
}