Symfony RestBundle:@View 注释在 v2 升级后停止工作

Symfony RestBundle: @View annotation stopped working after v2 upgrade

我认为 JMS 序列化程序和 FOSRestBundle 之间存在冲突:我得到一个空的 json 对象而不是 id 和 accessToken。 我是否缺少一些 v2 文档?

config.yml

fos_rest:
    routing_loader:
        default_format: json
        include_format: false
    format_listener: true
    view:
        view_response_listener: 'force'
        formats:
            json: true
        templating_formats:
            html: false
            json: false
    body_converter:
        enabled: true

控制器

class SecurityController extends FOSRestController
{
     *
     * @View(serializerGroups={"login"})
     *
     */
    public function postLoginAction(Request $request)
    {
            // $user = MyOAuthUserResponse extends AbstractUserResponse

           // before upgrade I just use: return $this->view($user);

            $view = $this->view($user);

            $context = new Context();
            $context->addGroup('login');

            $view->setContext($context);

            return $this->handleView($view);
    }

实体

/**
 * @Serializer\ExclusionPolicy("All")
 */
class User extends BaseUser
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Serializer\Expose()
     */
    protected $id;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created", type="datetime")
     * @Gedmo\Timestampable(on="create")
     */
    private $created;

    /**
     * @var string
     * @ORM\Column(type="string", nullable=true)
     * @Serializer\Expose()
     */
    private $accessToken;

找到问题了!

vendor/jms/serializer/src/JMS/Serializer/GraphNavigator.php:209

$exclusionStrategy = $context->getExclusionStrategy(); // Returns NULL

这似乎工作正常(升级前):

return $this->view($user);

但是自升级 $exclusionStrategy returns:

JMS\Serializer\Exclusion\GroupsExclusionStrategy Object
(
    [groups:JMS\Serializer\Exclusion\GroupsExclusionStrategy:private] => Array
        (
            [login] => 1
        )

    [nestedGroups:JMS\Serializer\Exclusion\GroupsExclusionStrategy:private] => 

)

为了解决这个问题,我删除了传递给视图的上下文代码,并将 view 传递给 handleview,例如:

return $this->handleView($this->view($user));

这个我误会了upgrade doc:

use FOS\RestBundle\Context\Context;

$view = new View();

$context = new Context();
$view->setContext($context);

$context = $view->getContext();