如何使 PATCH 字段在响应中是必需的,但在请求正文中是可选的?
我有一个实体,其中包括:
/**
* @ORM\Column(type="float", name="shipping_fees")
*/
#[Groups(["stock:read", "stock:write"])]
#[Assert\PositiveOrZero]
#[Assert\NotNull]
private float $shippingFees = 0.;
以及与该实体关联的 PATCH
请求。生成的 OpenAPI 上下文将 shippingFees
标记为 requestBody
和响应中的 required
属性。 (由于 NotNull
约束)
我希望该字段在请求正文中是可选的,因为这是一个 PATCH
请求。我怎样才能实现它?这是我应该报告的 API 平台故障吗?
I have an entity with, among other fields :
/**
* @ORM\Column(type="float", name="shipping_fees")
*/
#[Groups(["stock:read", "stock:write"])]
#[Assert\PositiveOrZero]
#[Assert\NotNull]
private float $shippingFees = 0.;
And a PATCH
request associated to this entity. The generated OpenAPI context marks shippingFees
as a required
property both in the requestBody
, and in the response. (because of the NotNull
constraint)
I expect the field to be optional in the request body, as this is a PATCH
request. How can I achieve it ? And is this a malfunction of API Platform which I should report ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅为
PATCH
方法创建单独的序列化组,例如stock:patch
。然后在Assert
注释中指定应应用它们的序列化组
。如下所示,因此
Assert\NotBlank
将仅适用于"stock:read","stock:write"
组。我希望这对你有帮助)Create a separate serialization group just for the
PATCH
method, such asstock:patch
. Then specify in theAssert
annotations the serializationgroups
in which they should be applied. As shown belowSo
Assert\NotBlank
will only apply to"stock:read","stock:write"
groups. I hope this helps you)