在 Symfony ApiPlatform 中具有相同类的属性 [英] Having property of a same class in Symfony ApiPlatform

查看:24
本文介绍了在 Symfony ApiPlatform 中具有相同类的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了 ApiPlatform SymfonyCasts,到目前为止我喜欢我可以用它做什么.但是,我遇到了以下情况:

I have gone over the ApiPlatform SymfonyCasts and so far I love what I can do with it. However, I have hit the wall with the following scenario:

  • 我有一个带有属性 baseContractContract 类,它指向一个新对象应该基于的对象.

  • I have a class Contract with property baseContract which points to an object on which the new object should be based upon.

denormalizationContextnormalizationContext 都为所有字段(目前)设置为 contracts:writecontracts:阅读,恭敬.

Both denormalizationContext and normalizationContext are set for all fields (for now) to contracts:write and contracts:read, respectfully.

当我转到 /api 时,GETPOST 端点都显示所有属性,但跳过 baseContract...

When I go to /api, both GET and POST endpoints show all properties, but skip the baseContract...

这有什么问题吗?

最基本的想法是我希望能够发布这样的内容:

The very basic idea is that I want to be able to post something like this:

{
  "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;

推荐答案

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

I have finally checked on my projet. As you said in the comments, it's indeed because of the recursion.

使用此序列化组配置,您可以嵌入您的 baseContract 属性.

With this serialization groups configuration, you're embedding your baseContract property.

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

Add an annotation @ApiProperty(readableLink=false, writableLink=false) to the $baseContract. It prevents the embedding behaviour and the swagger should display your field.

这篇关于在 Symfony ApiPlatform 中具有相同类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆