【问题标题】:Api platform - invalid IRI instead of validation errorApi 平台 - 无效 IRI 而不是验证错误
【发布时间】:2020-07-03 23:57:12
【问题描述】:

我正在为我的预订应用程序使用 API 平台。

Booking 与部门之间存在多对一的关系。

当我通过发送带有空字符串作为部门值的 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"
                        ]
                    }
                ]
            ]
        }, 
}

如果我遗漏了什么,请告知

【问题讨论】:

    标签: symfony4 api-platform.com symfony-validator


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2019-09-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 2016-05-01
      • 2018-10-18
      • 2013-01-06
      • 1970-01-01
      相关资源
      最近更新 更多