在 Symfony ApiPlatform 中有 属性 相同的 class
Having property of a same class in Symfony ApiPlatform
我已经了解了 ApiPlatform SymfonyCasts,到目前为止,我喜欢用它做的事情。但是,我遇到了以下情况:
我有一个 class Contract
和 属性 baseContract
,它指向新对象应基于的对象。
denormalizationContext
和 normalizationContext
所有字段(目前)都设置为 contracts:write
和 contracts:read
,恭敬。
当我转到 /api
时,GET
和 POST
端点都显示所有属性,但跳过 baseContract
...
这有什么问题吗?
最基本的想法是我希望能够 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 应该显示您的字段。
我已经了解了 ApiPlatform SymfonyCasts,到目前为止,我喜欢用它做的事情。但是,我遇到了以下情况:
我有一个 class
Contract
和 属性baseContract
,它指向新对象应基于的对象。denormalizationContext
和normalizationContext
所有字段(目前)都设置为contracts:write
和contracts:read
,恭敬。当我转到
/api
时,GET
和POST
端点都显示所有属性,但跳过baseContract
...
这有什么问题吗?
最基本的想法是我希望能够 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 应该显示您的字段。