Unable to override JMS Serializer's default handler for Symfony ConstraintViolationList

我怕爱的太早我们不能终老 提交于 2019-12-11 04:36:50

问题


I am unable to override default handlers in jms serializer bundle.

I'd like to change the way Symfony\Component\Validator\ConstraintViolationList is serialized so I wrote my own custom handler. And tagged it correctly as described in the documentation (and in various stackoverflow answers).

However, my handler keeps being overridden by the default handler for ConstraintViolationList that JMS Serializer bundle ships with.

I've tagged my handler service correctly. In fact, my handler service is detected and used correctly when I comment out ms_serializer.constraint_violation_handler service definition from vendor/jms/serializer-bundle/JMS/SerializerBundle/Resources/config/services.xml

How can I stop the default handler from overriding my custom one?

I've even tried overriding jms_serializer.constraint_violation_handler.class parameter from my own bundle but still no luck.

Here is my Handler class:

<?php
namespace Coanda\Bridge\JMSSerializer\Handler;

use JMS\Serializer\Context;
use JMS\Serializer\GraphNavigator;
use JMS\Serializer\Handler\SubscribingHandlerInterface;
use JMS\Serializer\JsonSerializationVisitor;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;

class ConstraintViolationHandler implements SubscribingHandlerInterface
{
    public static function getSubscribingMethods()
    {
        $methods = [];
        $methods[] = [
            'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
            'type' => ConstraintViolationList::class,
            'format' => 'json',
            'method' => 'serializeListToJson'
        ];
        return $methods;
    }

    public function serializeListToJson(
        JsonSerializationVisitor $visitor,
        ConstraintViolationList $list,
        array $type,
        Context $context
    ) {
        $violations = [];
        foreach ($list as $item) {
            $violations[$item->getPropertyPath()][] = $item->getMessage();
        }
        if (null === $visitor->getRoot()) {
            $visitor->setRoot($violations);
        }
        return $violations;
    }

}

I've registered it in my services.xml

    <service id="coanda.serializer.constraint_violation_handler"
        class="Coanda\Bridge\JMSSerializer\Handler\ConstraintViolationHandler">
        <tag name="jms_serializer.subscribing_handler"
            type="Symfony\Component\Validator\ConstraintViolationList"
            direction="serialization" format="json" method="serializeListToJson" />
    </service>

回答1:


This was happening because JMSSerializerBundle was registered after my bundle in AppKernel which meant that whatever service I define will be overridden by JMS Serializer's version of it.

Solution: put your bundle at the very bottom in AppKernel.php like so:

public function registerBundles()
{
    $bundles = [
        // .......
        new JMS\SerializerBundle\JMSSerializerBundle(),
        new My\Bundle\MyAwesomeBundle()
    ];

    return $bundles;
}


来源:https://stackoverflow.com/questions/37977816/unable-to-override-jms-serializers-default-handler-for-symfony-constraintviolat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!