代码之家  ›  专栏  ›  技术社区  ›  Andrei Herford

JMSSerializer事件不适用于具体类型

  •  1
  • Andrei Herford  · 技术社区  · 7 年前

    我正在尝试创建一个 EventSubscriber 对于不同的序列化事件,使用 Symfony 3.4.21 , JMSSerializer 2.1.0 JMSSerializerBundle 3.0.0 .

    问题是,订阅服务器只有在没有为具体类型注册时才工作。 为什么会这样?

    我的设置:

    // src/AppBundle\EventListener\SerializationEventSubscriber.php
    use JMS\Serializer\EventDispatcher\Events;
    use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
    use JMS\Serializer\EventDispatcher\PreSerializeEvent;
    use JMS\Serializer\EventDispatcher\ObjectEvent;
    
    class SerializationEventSubscriber implements EventSubscriberInterface {
        public static function getSubscribedEvents() {        
            return array(       
                // Subscribe with NO specific class       
                array('event' => Events::PRE_SERIALIZE, 'method' => 'onPreSerialize_NoClass'),
                array('event' => Events::POST_DESERIALIZE, 'method' => 'onPostDeserialize_NoClass'),
    
    
                // Subscribe WITH specific class       
                array('event' => Events::PRE_SERIALIZE, 'method' => 'onPreSerialize_Class', 'class' => 'AppBundle\Entity\MyEntity'),
                array('event' => Events::POST_DESERIALIZE, 'method' => 'onPostDeserialize_Class', 'class' => 'AppBundle\Entity\MyEntity'),
            );
        }
    
        public function onPreSerialize_NoClass(PreSerializeEvent $event) {
            // log...
        }
    
        public function onPostDeserialize_NoClass(PreSerializeEvent $event) {
            // log...
        }
    
        public function onPreSerialize_Class(PreSerializeEvent $event) {
            // log...
        }
    
        public function onPreSerialize_Class(PreSerializeEvent $event) {
            // log...
        }
    }
    
    
    // app/config/services.yml
    AppBundle\EventListener\SerializationEventSubscriber:
        tags:
            - { name: "jms_serializer.event_subscriber" }  
    

    虽然正确调用了前两个处理程序(定义时没有特定的类),但具有特定类的两个处理程序是 不叫 .

    如果我检查事件对象的类 ..._NoClass 处理程序,它与 ..._Class 处理程序。

    根据 docs 为一个特定的类定义一个处理程序应该没有问题。 我的配置有什么问题?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Andrei Herford    7 年前

    似乎这是一个错误 JMSSerializerBundle 3.0.0 . 对…的更新 3.1.0 解决了这个问题。

    推荐文章