Search code examples
symfonyswaggerapi-platform.comdenormalization

Api plateform denormalization on custom operation problem


I have an entity Voucher that contain a custom operation name 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;
    } 

}

I added the denormalization group to code,discount,starts_at, I Later to validate this columns in operations to do so I need to add it first in the denormalization context but in the swagger it show only the code and discount properties


Solution

  • It is because of the spelling combination of your $starts_at property and getStartsAt() getter. Basically, Symfony serializer use yours getters to access private properties.

    Just replace either your property by $starstAt or add an underscore to your getter.

    Take a look at this;