php 在ApiPlatform从3.1迁移到3.2之后,我的Denormalizer崩溃了,因为它被发送到ValidationExceptionNormalizer

xsuvu9jc  于 6个月前  发布在  PHP
关注(0)|答案(1)|浏览(39)

受Ryan Weaver关于SymfonyCast上ApiPlatform的精彩教程的启发,我创建了一个Normalizer和一个Denormalizer来管理组的规范化和反规范化。
GroupsDenormalizer装饰api_platform.serializer.normalizer.itemGroupsNormalizer装饰api_platform.jsonld.normalizer.item我使用#[AsDecorator]属性来完成它。
一切正常!我的测试覆盖了100%的代码。没有弃用。一旦我从ApiPlatform 3.1.20升级到3.2.0,我的代码就失败了,Api不再可用。
类型错误:ApiPlatform\Symfony\Validator\Serializer\ValidationExceptionNormalizer::__construct():参数#1($decorated)必须是Symfony\Component\Serializer\Normalizer\NormalizerInterface,App\Security\GroupsDenormalizer给定的类型,在/srv/app/var/cache/test/ContainerCco 4 qUx/App_KernelTestLoggContainer. php行2484调用
我不明白为什么依赖注入将我的反规范化器作为ValidationExceptionNormalizer构造函数的参数发送。
我试图找到关于从3.1迁移到3.2的文档(没有成功)。我试图更新api平台的配方。我试图改变优先级。我试图在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);
    }
}

个字符

axkjgtzd

axkjgtzd1#

我的getSupportedTypes没有正确排除很多类。

/**
     * @return array<string, bool>
     */
    public function getSupportedTypes(?string $format): array
    {
        return [
            Character::class => false,
            User::class => false,
            '*' => null,
        ];
    }

字符串

相关问题