Symfony:如何使 JMS Serializer 与严格类型一起工作?

Wil*_*ild 5 php serialization visitor symfony jmsserializerbundle

这是我的情况:

我正在尝试编写一个适用于“严格”类型(整数、布尔值和浮点数)的 Symfony REST API,因为默认的 Symfony 行为不支持它,我想避免强制转换类型(例如:JMS Serializer 转换字符串值成整数字段类型

为此,我创建了一个实现JMS\Serializer\Handler\SubscribingHandlerInterface (例如 a StrictIntegerHandler)的自定义处理程序:

<?php

namespace AppBundle\Serializer;

use JMS\Serializer\Context;
use JMS\Serializer\GraphNavigator;
use JMS\Serializer\Handler\SubscribingHandlerInterface;
use JMS\Serializer\JsonDeserializationVisitor;
use JMS\Serializer\JsonSerializationVisitor;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

class StrictIntegerHandler implements SubscribingHandlerInterface
{
    public static function getSubscribingMethods()
    {
        return [
            [
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
                'format' => 'json',
                'type' => 'strict_integer',
                'method' => 'deserializeStrictIntegerFromJSON',
            ],
            [
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
                'format' => 'json',
                'type' => 'strict_integer',
                'method' => 'serializeStrictIntegerToJSON',
            ],
        ];
    }

    public function deserializeStrictIntegerFromJSON(
        JsonDeserializationVisitor $visitor, $data, array $type)
    {
        return $data;
    }

    public function serializeStrictIntegerToJSON(
        JsonSerializationVisitor $visitor, $data, array $type, Context $context)
    {
        return $visitor->visitInteger($data, $type, $context);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的实体看起来:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use Symfony\Component\Validator\Constraints as Validator;

/**
 * Person
 *
 * @ORM\Table(name="persons")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\PersonRepository")
 */
class Person
{
    /**
     * @var int age
     *
     * @ORM\Column(name="age", type="integer")
     *
     * @Serializer\Type("strict_integer")
     * @Serializer\Groups({"Person"})
     *
     * @Validator\Type(type="integer", message="Age field has wrong type")
     */
    private $age;

    public function getAge()
    {
        return $this->age;
    }

    public function setAge(int $age)
    {
        $this->age = $age;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我抛出以下 POST 操作时,JMS Serializer 返回正确的结果:

  1. { "age" : 12 } 会导致 int(12)
  2. { "age" : "asdf" } 会导致 "Age field has wrong type"

在这两种情况下,我的方法都deserializeStrictIntegerFromJSON被调用,因此反序列化过程可以按照我的意愿完美运行。

序列化过程出现问题:当我启动 GET 操作 ( /person/id_person) 时,出现以下异常:

预期的对象,但得到了整数。您是否有错误的 @Type 映射,或者这可能是 Doctrine 多对多关系?(JMS\Serializer\Exception\LogicException)

调试堆栈跟踪显示我serializeStrictIntegerToJSON从未调用过该方法..

我该如何解决?谢谢。

Wil*_*ild 1

我找到了解决方案:我必须将jms/serializer库升级到版本 1.5.0。

我的问题是我正在使用jms/serializer(v1.1.0),它的SerializationContext类抛出了前一个LogicException方法isVisiting(),因为在类方法的strict_integerswitch-case 句子中无法识别类型。accept()GraphNavigator