Api Platform DateTimeNormalizer不允许为空

问题描述 投票:0回答:1

因为我喜欢用stackoverflow而不是github,所以我将issue对话移到这里。

例如,您可能有一个具有deletedAt字段的用户。由于某些业务原因,删除了用户,但由于其他原因,您可能需要通过将deletedAt字段设置为null来激活他。如果您尝试使用API​​平台执行此操作,则会出现此错误:

The data is either an empty string or null, you should pass a string that can be parsed with the passed format or a valid DateTime string.

问题是,处理它的最佳方法是什么?

php symfony api-platform.com
1个回答
0
投票

这是我的处理方式。

我扩展了API平台DateTimeNormalizer:

namespace App\Normalizer;

use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer as APIPlatformDateTimeNormalizer;

class DateTimeNormalizer extends APIPlatformDateTimeNormalizer
{
    public function denormalize($data, $type, $format = null, array $context = [])
    {
        if (null === $data) {
            return null;
        }

        parent::denormalize($data, $type, $format, $context);
    }
}

并且将其定义为与父规范化器同名的服务,而实际上它覆盖了它(api/config/services.yml):

services:
    serializer.normalizer.datetime:
            class: App\Normalizer\DateTimeNormalizer
            tags:
                - { name: serializer.normalizer }
© www.soinside.com 2019 - 2024. All rights reserved.