symfony4 - Api 平台 - 无效 IRI 而不是验证错误
问题描述
我正在为我的预订应用程序使用 API 平台。
预订与部门具有多对一的关系。
当我通过发送带有空字符串作为部门值的 POST 请求来创建新预订时,我预计会出现验证错误,但相反,我得到了“无效 IRI”
预订实体
/**
* @ApiResource(
* itemOperations={
* "get"={
* "normalization_context"={
* "groups"={"read-item"}
* }
* },
* "put"={
* "denormalization_context"={
* "groups"={"put"}
* }
* }
* }
* @ORM\Entity(repositoryClass="App\Repository\BookingRepository")
class Booking
{
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Department", inversedBy="bookings")
* @ORM\JoinColumn(nullable=false)
* @Assert\NotBlank
* @Groups({"read-item", "put"})
*/
private $department;
}
部门实体
/**
* @ApiResource(
* collectionOperations={"get"},
* itemOperations={"get"}
* )
* @ORM\Entity(repositoryClass="App\Repository\DepartmentRepository")
*/
class Department
{
/**
* @ORM\OneToMany(targetEntity="App\Entity\Booking", mappedBy="department")
*/
private $bookings;
}
例如创建预订的示例请求 (POST)
{
"department": ""
}
错误:
{
"@context": "/api/contexts/Error",
"@type": "hydra:Error",
"hydra:title": "An error occurred",
"hydra:description": "Invalid IRI \"\".",
"trace": [
{
"namespace": "",
"short_class": "",
"class": "",
"type": "",
"function": "",
"file": "/var/www/html/vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php",
"line": 418,
"args": []
},
{
"namespace": "ApiPlatform\\Core\\Serializer",
"short_class": "AbstractItemNormalizer",
"class": "ApiPlatform\\Core\\Serializer\\AbstractItemNormalizer",
"type": "->",
"function": "denormalizeRelation",
"file": "/var/www/html/vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php",
"line": 685,
"args": [
[
"string",
"department"
],
[
"object",
"ApiPlatform\\Core\\Metadata\\Property\\PropertyMetadata"
],
[
"string",
"App\\Entity\\Department"
],
[
"string",
""
],
[
"string",
"json"
],
[
"array",
{
"operation_type": [
"string",
"collection"
],
"collection_operation_name": [
"string",
"post"
],
"api_allow_update": [
"boolean",
false
],
"resource_class": [
"string",
"App\\Entity\\Department"
],
"input": [
"null",
null
],
"output": [
"null",
null
],
"request_uri": [
"string",
"/api/bookings"
],
"uri": [
"string",
"http://localhost/api/bookings"
],
"skip_null_values": [
"boolean",
true
],
"api_denormalize": [
"boolean",
true
],
"cache_key": [
"string",
"db505854694e53aec6831eb427a03028"
]
}
]
]
},
}
请告知我是否遗漏了什么
解决方案
不确定这是否会解决它,但您可以尝试在 @ApiResource 注释中显式添加 iri;过去为我修复了类似的错误:
@ApiResource(
iri="bookings",
itemOperations={
"get"={
"normalization_context"={
"groups"={"read-item"}
}
},
"put"={
"denormalization_context"={
"groups"={"put"}
}
}
}
)
和
@ApiResource(
iri="departments",
collectionOperations={"get"},
itemOperations={"get"}
)
推荐阅读
- python - 与烧瓶项目的 href 链接出现错误 404。似乎找不到页面
- javascript - 具有折线图的虚拟测量设备
- swift - 了解不安全 swift 中数据复制的性能
- python - 如何从 csv 文件中绘制列的直方图
- python - Python计算我在一个列表中有多少重复项,其中有很多列表
- python - 使用字符串进行迭代是否有效,但使用整数进行迭代不起作用?
- php - 在服务器上安装 laravel 时出现致命的 PHP 错误
- azure - 如何将数组转换为 ARM 模板中一个对象的属性?
- circuit-sdk - 在昨天运行良好的 API 上出现“500”错误
- javascript - Javascript 使用 php 会话获取服务器数据