symfony - API 平台 Symfony,在使用 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;
}
我使用邮递员测试了 api,我得到了 500 内部服务器错误:
"hydra:description": "无法自动验证 "boolean" 类型的值。请提供约束。"
解决方案
为什么会出现此错误消息?
invoke()
方法必须返回:
- 一个
Symfony\Component\HttpFoundation\ResponseResponse
实例, - 目标实体的一个实例(似乎
AppUser
在这种情况下)。
在您的情况下,该方法返回true
; 由于验证是在控制器之后进行的,Api-Platform 尝试验证这个布尔值,这是不可能的。它需要一个实体。
关于问题中显示的代码
我仍然不清楚您要达到的目标:
- 为什么
$user
从不使用 arg? - 发送电子邮件后,您想保存任何实体吗?
- 为什么要获取
Request
内容?
推荐阅读
- c# - .NET Core/ASP.NET Core/ASP.NET Core MVC Web 应用实际使用
- excel - 添加扩展选择功能以对 VBA 宏中的列表进行排序功能
- batch-file - 在cmd中使用for循环将串口的输出管道化以卷曲
- delphi - 当我将数字分配给 Double 或 Extended 变量时,为什么会得到不正确的值?
- c - 使用 VS Code、Makefile 和自定义 bash 脚本在 C 语言中调试头文件
- typescript - TS 编译器 API:防止导出。出现在没有导出修饰符的节点上
- neovim - 如何查询当前工作目录的 neovim API?
- android - 将图像从 api 存储到房间数据库中
- c# - 使用来自 UserControl 资源文件的字符串资源
- amazon-web-services - 如何更改我的 Lambda 函数 API 端点的名称