Symfony 选民 - POST 操作发送空对象

Symfony voter - POST opreration sends null object

我正在与 Symfony5ApiPlatformphpunit

合作

这是我的实体 POST 带有注释的规则:

 *        "post"={
 *              "method"="POST",
 *              "validation_groups"={"create"},
 *              "security"="is_granted('POST', object)",
 *              "normalization_context"={"groups"={"entityname:write"}},
 *        }

我正在 运行 测试 POST 操作:

$body = '{
  "debut": "2020-11-17T14:03:10.208Z",
  "fin": "2020-11-17T14:03:10.208Z"
}';

$res = $this->buildPostPutRequest(
  Actions::POST,
  GenericRoutes::ROUTE_NAME,
  $body,
  GenericCredentials::USER_CREDENTIALS
);

$this->assertResponseStatusCodeSame(403);

这是我的 voter:

protected function supports($attribute, $subject)
{
    return parent::supports($attribute, $subject)
        && ($subject instanceof \App\Entity\EntityType || 
        $this->arrayOf($subject, \App\Entity\EntityType::class));
}

protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
  // Some code
}

现在我确实收到了 403 但原因不对。

测试连voteOnAttribute函数都不输入,因为supports函数return false.

如果我转储我在 supports 中收到的 $attribute$subjet 变量,我得到:

'POST'
null

我什么时候应该收到:

'POST'
EntityType

有人知道为什么我收到的是 null 而不是实体类型吗?

由于 lifecycle,您收到 null,将 security 注释 属性 替换为 security_post_denormalize