在 Symfony ApiPlatform 中有 属性 相同的 class

Having property of a same class in Symfony ApiPlatform

我已经了解了 ApiPlatform SymfonyCasts,到目前为止,我喜欢用它做的事情。但是,我遇到了以下情况:

这有什么问题吗?

最基本的想法是我希望能够 post 这样的事情:

{
  "name": "string",
  "description": "string",
  "contractNo": "string",
  "baseContract": "/api/contacts/{some_id}
}
    /**
     * @ORM\Entity(repositoryClass=ContractRepository::class)
     *
     * @ApiResource(
     *     normalizationContext={"groups": "contract:read"},
     *     denormalizationContext={"groups": "contract:write"},
     *     collectionOperations={
     *          "get",
     *          "post"
     *     },
     *     itemOperations={
     *          "get"
     *     }
     * )
     */
    class Contract
    {
......
        /**
         * @ORM\ManyToOne(targetEntity=Contract::class)
         * @ORM\JoinColumn()
         *
         * @Groups({"contract:read", "contract:write"})
         */
        private ?Contract $baseContract;

我终于检查了我的项目。正如你在评论中所说,确实是因为递归。

使用此序列化组配置,您就是 embedding 您的 baseContract 属性。

向 $baseContract 添加注释 @ApiProperty(readableLink=false, writableLink=false)。它可以防止嵌入行为,并且 swagger 应该显示您的字段。