API Platform Symfony,在使用 sarbacane 发送短信时无法验证布尔类型的值
API Platform Symfony, cannot validate values of type boolean when send sms with sarbacane
我创建了一个自定义操作来使用 symfony 和 sarbacane 发送短信:
在我的 AppUser
实体中,我添加了注释:
* "GET",
* "PUT",
* "PATCH",
* "DELETE",
* "send_sms"={
* "method"="POST",
* "path"="/app_users/{id}/sms",
* "controller"=SmsController::class,
* "normalization_context"={"groups"={"user:read"}},
* "put"={"validation_groups"={"Default", "sedValidation"}}
* }
* }
在我的控制器中,我实现了调用方法:
public function __invoke(AppUser $user, Request $request, SerializerInterface $serializer) : bool
{
$data = $request->getContent();
// json decode transforms to object by default
// add true
$json_encode = json_decode($data, true);
$content = $json_encode['content'];
$currentUser = $this->getUser();
$currentUserPhone = $currentUser->getPhone();
$res = $this->sarbacaneApiHelper->call('campaigns/sms', [
'name' => sprintf("eXpanded n°%s", uniqid()),
'kind' => 'SMS_NOTIFICATION',
'smsFrom' => "eXpanded", // entre 3 et 11 caractères alpha-numériques
'content' => $content, // max 450 caractères
]);
$phone = $currentUserPhone;
$sarbacaneCampaignId = $res->id;
// Ajoute des destinataires à la campagne Sarbacane
$res = $this->sarbacaneApiHelper->call(sprintf('campaigns/%s/recipients', $sarbacaneCampaignId), [
[
'phone' => $phone,
],
]);
$params = [
"phone" => $currentUserPhone,
];
$this->sarbacaneApiHelper->call(sprintf('campaigns/%s/send', $sarbacaneCampaignId), $params);
$sent = true;
return $sent;
}
我使用 postman 测试了 api,我得到了 500 个内部服务器错误:
"hydra:description": "Cannot validate values of type "boolean" automatically. Please provide a constraint."
为什么会出现这个错误信息?
一个invoke()
方法must return either:
- 一个
Symfony\Component\HttpFoundation\ResponseResponse
实例,
- 目标实体的实例(在本例中似乎是
AppUser
)。
在你的例子中,方法 returns true
;由于验证是在控制器之后进行的,Api-Platform 尝试验证此布尔值,但这是不可能的。它需要一个实体。
关于问题中显示的代码
我还不清楚你想要达到什么目的:
- 为什么从未使用
$user
arg?
- 发送 e-mail 后是否要保存任何实体?
- 为什么要获取
Request
内容?
我创建了一个自定义操作来使用 symfony 和 sarbacane 发送短信:
在我的 AppUser
实体中,我添加了注释:
* "GET",
* "PUT",
* "PATCH",
* "DELETE",
* "send_sms"={
* "method"="POST",
* "path"="/app_users/{id}/sms",
* "controller"=SmsController::class,
* "normalization_context"={"groups"={"user:read"}},
* "put"={"validation_groups"={"Default", "sedValidation"}}
* }
* }
在我的控制器中,我实现了调用方法:
public function __invoke(AppUser $user, Request $request, SerializerInterface $serializer) : bool
{
$data = $request->getContent();
// json decode transforms to object by default
// add true
$json_encode = json_decode($data, true);
$content = $json_encode['content'];
$currentUser = $this->getUser();
$currentUserPhone = $currentUser->getPhone();
$res = $this->sarbacaneApiHelper->call('campaigns/sms', [
'name' => sprintf("eXpanded n°%s", uniqid()),
'kind' => 'SMS_NOTIFICATION',
'smsFrom' => "eXpanded", // entre 3 et 11 caractères alpha-numériques
'content' => $content, // max 450 caractères
]);
$phone = $currentUserPhone;
$sarbacaneCampaignId = $res->id;
// Ajoute des destinataires à la campagne Sarbacane
$res = $this->sarbacaneApiHelper->call(sprintf('campaigns/%s/recipients', $sarbacaneCampaignId), [
[
'phone' => $phone,
],
]);
$params = [
"phone" => $currentUserPhone,
];
$this->sarbacaneApiHelper->call(sprintf('campaigns/%s/send', $sarbacaneCampaignId), $params);
$sent = true;
return $sent;
}
我使用 postman 测试了 api,我得到了 500 个内部服务器错误:
"hydra:description": "Cannot validate values of type "boolean" automatically. Please provide a constraint."
为什么会出现这个错误信息?
一个invoke()
方法must return either:
- 一个
Symfony\Component\HttpFoundation\ResponseResponse
实例, - 目标实体的实例(在本例中似乎是
AppUser
)。
在你的例子中,方法 returns true
;由于验证是在控制器之后进行的,Api-Platform 尝试验证此布尔值,但这是不可能的。它需要一个实体。
关于问题中显示的代码
我还不清楚你想要达到什么目的:
- 为什么从未使用
$user
arg? - 发送 e-mail 后是否要保存任何实体?
- 为什么要获取
Request
内容?