在Symfony ApiPlatform中具有相同类的属性 [英] Having property of a same class in 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:
-
我有一个具有属性
baseContract
的类Contract
,它指向新对象应基于的对象.
I have a class
Contract
with propertybaseContract
which points to an object on which the new object should be based upon.
denormalizationContext
和 normalizationContext
都已为所有字段设置(现在)以 contracts:write
和 contracts:仔细阅读
.
Both denormalizationContext
and normalizationContext
are set for all fields (for now) to contracts:write
and contracts:read
, respectfully.
当我转到/api
时, GET
和 POST
端点都显示所有属性,但跳过 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;
推荐答案
我终于检查了我的projet.正如您在评论中所说,确实是因为递归.
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(visibleLink = false,writableLink = false).它可以防止嵌入行为,并且应该显示您的字段.
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屋!