API Platform serialization groups with multiple views

30 Views Asked by At

I'm sure I'm probably misunderstanding the documentation or the point of serialization groups. But I'd like to be able to specify that, for example, an index of reports not include the JSON data of the report, but that a full view of the report obviously do. But while I think based on that and my reading, the following definition should work correctly, I only get a full listing of all fields? Here is my entity:

<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use ApiPlatform\Doctrine\Odm\Filter\RangeFilter;
use App\Repository\ReportRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

#[ApiResource(
    description: "SPOT Usage Report",
    collectionOperations: [
        'get' => [
            'method' => 'GET',
            'groups' => ['report:index'],
        ],
        'get_daily' => [
            'method' => 'GET',
            'uriTemplate' => '/report/daily',
            'controller' => 'App\Controller\Reports\GetDailyReport',
            'groups' => ['report:read'],
        ],
        'post' => [
            'name' => 'generate_report',
            'method' => 'POST',
            'uriTemplate' => '/report/generate/',
            'controller' => 'App\Controller\Reports\GenerateReport',
            'validate' => false,
            'deserialize' => false,
            'serialize' => false,
        ],
    ],
    itemOperations: [
        'get' => [
            'method' => 'GET',
            'groups' => ['report:read']
        ],
        'post' => [
            'name' => 'generate_a_report',
            'method' => 'POST',
            'uriTemplate' => '/report/generate/{effective_date}',
            'controller' => 'App\Controller\Reports\GenerateReport',
            'validate' => false,
            'deserialize' => false,
            'serialize' => false,
        ],
    ],
    denormalizationContext: ['groups' => ['report:write']],
    formats: ['json', 'jsonld', 'html', 'csv' => ['text/csv']],
    normalizationContext: ['groups' => ['report:index', 'report:read']],
    security: "is_granted('ROLE_USER')",
)]
#[ApiFilter(PropertyFilter::class)]
#[ORM\Entity(repositoryClass: ReportRepository::class)]
class Report
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    /**
     *  The date for which the report is drawn.
     */
    #[ORM\Column(type: Types::DATE_MUTABLE)]
    #[Groups(['report:read', 'report:index'])]
    #[ApiFilter(RangeFilter::class)]
    private ?\DateTimeInterface $effective_date = null;

    /**
     *  The period for which the report is drawn.
     */
    #[ApiFilter(SearchFilter::class, strategy: 'partial')]
    #[Groups(['report:read', 'report:index'])]
    #[ORM\Column(length: 255)]
    private ?string $period = null;

    /**
     *  The date the report was created.
     */
    #[ApiFilter(RangeFilter::class)]
    #[ORM\Column(type: Types::DATETIME_MUTABLE)]
    #[Groups(['report:read'])]
    private ?\DateTimeInterface $created = null;

    /**
     *  The date the report was last modified.
     */
    #[ApiFilter(RangeFilter::class)]
    #[ORM\Column(type: Types::DATETIME_MUTABLE)]
    #[Groups(['report:read'])]
    private ?\DateTimeInterface $modified = null;

    /**
     *  The JSON data for the report.
     */
    #[ORM\Column(nullable: true)]
    #[Groups(['report:read'])]
    private ?array $json_data = null;

    public function __construct()
    {
        $this->created = new \DateTime();
        $this->modified = new \DateTime();
    }

    /**
     * @return int|null
     */
    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * Return the date for which the report is drawn.
     * @return \DateTimeInterface|null
     */
    public function getEffectiveDate(): ?\DateTimeInterface
    {
        return $this->effective_date;
    }

    /**
     * Return the period for which the report is drawn.
     * @return string|null
     */
    public function getPeriod(): ?string
    {
        return $this->period;
    }

    /**
     * Return the date the report was created.
     * @return \DateTimeInterface|null
     */
    public function getCreated(): ?\DateTimeInterface
    {
        return $this->created;
    }

    /**
     * Return the date the report was last modified.
     * @return \DateTimeInterface|null
     */
    public function getModified(): ?\DateTimeInterface
    {
        return $this->modified;
    }

    /**
     * Return the JSON data for the report.
     * @return array|null
     */
    public function getJsonData(): ?array
    {
        return $this->json_data;
    }

    public function setEffectiveDate(?\DateTimeInterface $effective_date): void
    {
        $this->effective_date = $effective_date;
    }

    public function setPeriod(?string $period): void
    {
        $this->period = $period;
    }

    public function setJsonData(?array $json_data): void
    {
        $this->json_data = $json_data;
    }
}

I expected those fields marked as report:index to show up only on the operations whose groups include the report:index group. Expected that therefore, the GET URL /reports would not include the json_data field.

0

There are 0 best solutions below