首页 > 解决方案 > 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"
                        ]
                    }
                ]
            ]
        }, 
}

请告知我是否遗漏了什么

标签: symfony4api-platform.comsymfony-validator

解决方案


不确定这是否会解决它,但您可以尝试在 @ApiResource 注释中显式添加 iri;过去为我修复了类似的错误:

 @ApiResource(
      iri="bookings",
      itemOperations={
          "get"={
              "normalization_context"={
                  "groups"={"read-item"}
              }
          },
          "put"={
              "denormalization_context"={
                  "groups"={"put"}
              }
          }
    }
 )

 @ApiResource(
      iri="departments",
      collectionOperations={"get"},
      itemOperations={"get"}
 )

推荐阅读