"Unable to generate an IRI for the item of type" api-平台次要版本更新后出现异常

"Unable to generate an IRI for the item of type" exception after api-platform minor version update

我在更新我的依赖项时偶然发现了一个问题,我想知道是否有人知道发生了什么......

我已将原因缩小到与 api-platformsymfony/routing 依赖项相关的问题。

版本是:

api-platform/core                       v2.3.0
symfony/routing                         v4.1.3

更新后:

api-platform/core                       v2.4.6
symfony/routing                         v4.1.12

错误是:

{
    "@context": "/api/v1/contexts/Error",
    "@type": "hydra:Error",
    "hydra:title": "An error occurred",
    "hydra:description": "Unable to generate an IRI for the item of type \"App\Entity\LeadRequest\"",
    "trace": [
        {
            "namespace": "",
            "short_class": "",
            "class": "",
            "type": "",
            "function": "",
            "file": "/var/www/html/vendor/api-platform/core/src/Bridge/Symfony/Routing/IriConverter.php",
            "line": 133,
            "args": []
        }
    [...]

实体看起来像:

/**
 * Class LeadRequest
 * @package App\Entity
 * @ApiResource(
 *     collectionOperations={"post"},
 *     itemOperations={
 *         "get"
 *     }
 * )
 * @ORM\Entity(repositoryClass="App\Repository\LeadRequestRepository")
 * @ORM\Table(name="lead_request")
 */
class LeadRequest

当然,我已尽职谷歌搜索,发现了多篇关于相同错误的帖子,但我已经尝试了他们的解决方案,但没有奏效。采取一些措施:

路由顺序看起来很好:

------------------------------------------- -------- -------- ------ ---------------------------------------------- 
Name                                        Method   Scheme   Host   Path                                          
------------------------------------------- -------- -------- ------ ---------------------------------------------- 
api_entrypoint                              ANY      ANY      ANY    /api/v1/{index}.{_format}                     
api_doc                                     ANY      ANY      ANY    /api/v1/docs.{_format}                        
api_jsonld_context                          ANY      ANY      ANY    /api/v1/contexts/{shortName}.{_format}        
api_lead_requests_post_collection           POST     ANY      ANY    /api/v1/lead_requests.{_format}               
api_lead_requests_get_item                  GET      ANY      ANY    /api/v1/lead_requests/{id}.{_format}    

实体 ID getter 可访问:

    /**
     * @var \Ramsey\Uuid\UuidInterface
     *
     * @ORM\Id
     * @ORM\Column(type="uuid", unique=true)
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
     */
    private $id;

    [...]

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

经过一些调试和研究,问题似乎是由实体 DataPersisterInterface 实现上的错误 persist() 方法引起的。

基本上id是空值,所以抛出异常:

Unable to generate an IRI for the item of type \"App\Entity\LeadRequest\

发生这种情况是因为从未保存实体,因此从未生成 ID。

将以下代码添加到方法实现中解决了问题:

class LeadRequest implements DataPersisterInterface
{
    [...]

    public function persist($data)
    {
        [...]

        $this->em->persist($data);
        $this->em->flush();

        return $data;
    }

    [...]
}

我仍然不知道为什么以前版本的库的测试是绿色的。

如果我找到有关该主题的更多信息,我将编辑此答案。

这种标识符默认是不允许的,只有"incremental ids".

但您可以添加自己的标识符规范化器(https://api-platform.com/docs/core/identifiers/#custom-identifier-normalizer

或者更多,您的自定义数据提供者。