Winzza
Winzza

Reputation: 582

The "Parent" property does not appear in "Example Value" with the denormalizationContext group

I use api-platform v3 and symfony v5. I have problem with displayed properties. For examle we have simple entity class Category:

<?php

declare(strict_types=1);

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
 */
class Category
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $title;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="children")
     */
    private $parent;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Category", mappedBy="parent")
     */
    private $children;

    public function __construct()
    {
        $this->children = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }

    public function getParent(): ?self
    {
        return $this->parent;
    }

    public function setParent(?self $parent): self
    {
        $this->parent = $parent;

        return $this;
    }

    /**
     * @return Collection|self[]
     */
    public function getChildren(): Collection
    {
        return $this->children;
    }

    public function addChild(self $child): self
    {
        if (!$this->children->contains($child)) {
            $this->children[] = $child;
            $child->setParent($this);
        }

        return $this;
    }

    public function removeChild(self $child): self
    {
        if ($this->children->contains($child)) {
            $this->children->removeElement($child);
            // set the owning side to null (unless already changed)
            if ($child->getParent() === $this) {
                $child->setParent(null);
            }
        }

        return $this;
    }
}

So far, we see the correct and logical "Example Value": Intermediate result

Now I want to display the title and parent properties for reading and writing. To do this, I enter group annotations:

<?php

declare(strict_types=1);

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ApiResource(
 *     normalizationContext={"groups" = {"category:read"}},
 *     denormalizationContext={"groups" = {"category:write"}}
 * )
 * @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
 */
class Category
{
    //...

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"category:read", "category:write"})
     */
    private $title;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="children")
     * @Groups({"category:read", "category:write"})
     */
    private $parent;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Category", mappedBy="parent")
     */
    private $children;

    //...
}

After that, we see the "title" property in the swagger documentation. But the "parent" property is not visible: Result after added groups annotation

Why can't I see the "parent" property in the "Example value" section?

As temporary solution I describe the documentation swagger:

<?php

declare(strict_types=1);

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ApiResource(
 *     collectionOperations={
 *         "get",
 *         "post" = {
 *             "sequrity" = "is_granted('ROLE_ADMIN')",
 *             "openapi_context" = {
 *                 "requestBody" = {
 *                     "content" = {
 *                         "application/json" = {
 *                             "schema" = {
 *                                 "type" = "object",
 *                                 "required" = {
 *                                     "title"
 *                                 },
 *                                 "properties" = {
 *                                     "title" = {
 *                                         "type" = "string"
 *                                     },
 *                                     "parent" = {
 *                                         "type" = "string"
 *                                     }
 *                                 }
 *                             }
 *                         }
 *                     }
 *                 }
 *             }
 *         }
 *     },
 *     normalizationContext={"groups" = {"category:read"}},
 *     denormalizationContext={"groups" = {"category:write"}}
 * )
 * @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
 */
class Category
{
    //...
}

I want to know how to do it right. Why groups do not work for "parent" property?

Upvotes: 3

Views: 839

Answers (1)

Had the same issue right now. Try to use annotation @ApiProperty(readableLink=false) for $parent property.

/**
 * @ORM\ManyToOne(targetEntity=CounterpartCategory::class, inversedBy="children")
 * @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
 * @Groups({"category:read", "category:write"})
 * @ApiProperty(readableLink=false)
 */
private ?self $parent;

Upvotes: 1

Related Questions