Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

352
Views
Symfony ApiPlatform: Role as Serializer/Groups in Entity is good idea?

I have many roles in the app, incl. super_admin, admin, user, moderator, editor ... I want to use apiPlatform and display appropriate fields in entities appropriately for roles, I also made additional classes converting roles into classes. everything works. However, it breaks the idea of groups in the serializer. For example: I have several entities that are related to each other. When I am an admin and want to download entities, all properties with the "admin: read" group will download + all properties from the related classes also with the "admin: read" group, even if I do not use them. Is it okay? Is it a good idea to use roles in entities? There is another better way?

Example from https://api-platform.com/docs/core/serialization/

Class Book

<?php
// api/src/Entity/Book.php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;

#[ApiResource(normalizationContext: ['groups' => ['book']])]
class Book
{
    /**
     * @Groups({"book"})
     */
    public $name;

    /**
     * @Groups({"book"})
     */
    public $author;

    // ...
}

Class Person

<?php
// api/src/Entity/Person.php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;

#[ApiResource]
class Person
{
    /**
     * ...
     * @Groups("book")
     */
    public $name;

    // ...
}

And result

{
  "@context": "/contexts/Book",
  "@id": "/books/62",
  "@type": "Book",
  "name": "My awesome book",
  "author": {
    "@id": "/people/59",
    "@type": "Person",
    "name": "Kévin Dunglas"
  }
}

So if instead of "book" you type "admin: read" it will always serialize everything that has a group "admin: read". I think it's getting unnecessary data. I want to emphasize that there are a lot of entities, not like in the example of two / several.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I think it's a good idea to add role in the serialization but when you add a role in serialization you should decorate the api_platform.serializer.context_builder see this section https://api-platform.com/docs/core/serialization/#changing-the-serialization-context-dynamically

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!