Api 自定义操作问题的平台反规范化

Api plateform denormalization on custom operation problem

我有一个包含自定义操作名称的实体凭证 add_voucher:

<?php
/**
 * @ApiResource(
 *     collectionOperations={
 *          "add_voucher"={
 *                 "access_control"="is_granted('ROLE_COMMERCIAL')",
 *                 "method"="POST",
 *                 "path"="/vouchers/add-new",
 *                 "controller"=AddVoucherAction::class,
 *                  "denormalization_context"={
 *                      "groups"={"add_new_voucher"}
 *               },
 *                  "validation_groups"={"Default", "add_voucher_validation"}
 *         },
 *     }
 * )
 * @ORM\Entity(repositoryClass="App\Repository\VoucherRepository", repositoryClass=VoucherRepository::class)
 */
class Voucher
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @Groups("add_new_voucher")
     * @ORM\Column(type="string", length=255, unique=true)
     */
    private $code;
    /**
     * @Groups("add_new_voucher")
     * @ORM\Column(type="integer")
     */
    private $discount;
    /**
     * @Groups("add_new_voucher")
     * @ORM\Column(type="datetime")
     */
    private $starts_at;

    public function getDiscount()
    {
        return $this->discount;
    }
    public function setDiscount($discount): void
    {
        $this->discount = $discount;
    }
    public function getId(): ?int
    {
        return $this->id;
    }
    public function getCode(): ?string
    {
        return $this->code;
    }
    public function setCode(string $code): self
    {
        $this->code = $code;
        return $this;
    }
    public function getStartsAt(): ?\DateTimeInterface
    {
        return $this->starts_at;
    }
    public function setStartsAt(\DateTimeInterface $starts_at): self
    {
        $this->starts_at = $starts_at;
        return $this;
    } 

}

我将非规范化组添加到 code,discount,starts_at,我稍后要在操作中验证此列,我需要先在非规范化上下文中添加它,但在 swagger 中它只显示 代码和折扣属性

是因为你的$starts_at属性和getStartsAt()的拼写组合getter。基本上,Symfony 序列化器使用你的 getters 来访问私有属性。

只需将 属性 替换为 $starstAt 或在 getter.

中添加下划线

看看this;