Skip to content
Advertisement

API Platform -Serialization groups don’t work correctly

I have a project developped under Symfony 5 and API Platform. However, I encounter a very strange problem. When I add serialization or deserialization groups to my resources and I try to recover a collection or even an item, all the fields do not appear in the response.

That is my resource definition

<?php

namespace AppEntity;

use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
use ApiPlatformCoreAnnotationApiResource;
use SymfonyComponentSerializerAnnotationGroups;
use SymfonyComponentValidatorConstraints as Assert;

/**
 * @ApiResource(
 *  attributes={"security"="is_granted('ROLE_USER')"},
 *  normalizationContext={
 *      "groups"={"pelerins_read"}
 *  },
 *  collectionOperations={
 *      "get"={"security"="is_granted('ROLE_INSCRIPTION') or is_granted('ROLE_ENCADREUR')", "security_message"="Vous n'êtes autorisés à consulter cette ressource"},
 *      "post"={"security"="is_granted('ROLE_INSCRIPTION')", "security_message"="Vous n'êtes autorisés à consulter cette ressource"}
 *  },
 *  itemOperations={
 *      "get"={"security"="is_granted('ROLE_INSCRIPTION') or is_granted('ROLE_ENCADREUR')"},
 *      "put"={"security"="is_granted('ROLE_INSCRIPTION')"}
 *  }
 * )
 * @ORMEntity(repositoryClass="AppRepositoryPelerinRepository")
 */
class Pelerin
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     * @Groups({"pelerins_read", "pelerins_read", "etat_sante_read"})
     */
    private $id;

    /**
     * @ORMColumn(type="string", length=255)
     * @AssertNotBlank(message="Le nom de famille du pèlerin est recquis.")
     * @Groups({"pelerins_read", "pelerins_read", "etat_sante_read"})
     */
    private $nom;

    /**
     * @ORMColumn(type="string", length=255)
     * @AssertNotBlank(message="Le prénom du pèlerin est recquis.")
     * @Groups({"pelerins_read", "pelerins_read", "etat_sante_read"})
     */
    private $prenom;

    /**
     * @ORMColumn(type="string", length=255)
     * @AssertNotBlank(message="Le sexe du pèlerin est recquis.")
     * @AssertChoice(choices={"M", "F"}, message="Le sexe doit un caractère entre M et F")
     * @Groups({"pelerins_read"})
     */
    private $sexe;

    /**
     * @ORMColumn(type="datetime")
     * @AssertNotBlank(message="La date de naissance du pèlerin est recquise.")
     * @Groups({"pelerins_read"})
     */
    private $date_naissance;

    /**
     * @ORMColumn(type="string", length=255)
     * @AssertNotBlank(message="Le lieu de naissance du pèlerin est recquis.")
     * @Groups({"pelerins_read"})
     */
    private $lieu_naissance;

    /**
     * @ORMColumn(type="string", length=255)
     * @AssertNotBlank(message="La résidence du pèlerin est recquise.")
     * @Groups({"pelerins_read"})
     */
    private $residence;

    /**
     * @ORMColumn(type="string", length=255, nullable=true)
     * @Groups({"pelerins_read"})
     */
    private $numero_telephone;

    /**
     * @ORMColumn(type="string", length=255, nullable=true)
     * @AssertEmail(message="Le format de l'adresse email saisie n'est pas correct")
     * @Groups({"pelerins_read"})
     */
    private $email;

    /**
     * @ORMColumn(type="string", length=255, nullable=true)
     * @Groups({"pelerins_read"})
     */
    private $numero_whatsapp;

    /**
     * @ORMColumn(type="string", length=255, nullable=true)
     * @AssertNotBlank(message="Le numéro du passeport du pèlerin est recquis")
     * @Groups({"pelerins_read"})
     */
    private $numero_passeport;

    /**
     * @ORMColumn(type="string", length=255, nullable=true)
     * @AssertNotBlank(message="La nature du passeport du pèlerin est recquis")
     * @Groups({"pelerins_read"})
     */
    private $nature_passeport;

    /**
     * @ORMManyToOne(targetEntity="AppEntityFormule", inversedBy="pelerins")
     * @ORMJoinColumn(nullable=false)
     * @Groups({"pelerins_read"})
     * @AssertNotBlank(message="Formule de voyage recquise pour continuer l'opération")
     */
    private $formule;

    /**
     * @ORMColumn(type="datetime")
     * @Groups({"pelerins_read"})
     * @AssertNotBlank(message="La date d'inscription est requise")
     */
    private $date_inscription;

    /**
     * @ORMColumn(type="string", length=255, nullable=true)
     * @Groups({"pelerins_read"})
     */
    private $inscrit_par;

    /**
     * @ORMColumn(type="string", length=255, nullable=true)
     * @Groups({"pelerins_read"})
     */
    private $numero_inscrit_par;

    /**
     * @ORMColumn(type="string", length=255, nullable=true)
     * @Groups({"pelerins_read"})
     */
    private $en_cas_urgence;

    /**
     * @ORMColumn(type="string", length=255, nullable=true)
     * @Groups({"pelerins_read"})
     */
    private $numero_en_cas_urgence;

    /**
     * @ORMColumn(type="string", length=500, nullable=true)
     * @Groups({"pelerins_read"})
     */
    private $commentaire;

    /**
     * @ORMColumn(type="datetime")
     * @Groups({"pelerins_read"})
     */
    private $created_at;

    /**
     * @ORMManyToOne(targetEntity="AppEntityUser")
     * @ORMJoinColumn(nullable=false)
     * @Groups({"pelerins_read"})
     */
    private $created_by;

    /**
     * @ORMColumn(type="datetime", nullable=true)
     * @Groups({"pelerins_read"})
     */
    private $updated_at;

    /**
     * @ORMManyToOne(targetEntity="AppEntityUser")
     * @Groups({"pelerins_read"})
     */
    private $updated_by;

    /**
     * @ORMColumn(type="boolean")
     * @Groups({"pelerins_read"})
     */
    private $is_deleted;

    /**
     * @ORMColumn(type="datetime", nullable=true)
     * @Groups({"pelerins_read"})
     */
    private $deleted_at;

You can see this picture, only

{
  "nom": "string",
  "prenom": "string",
  "sexe": "string",
  "residence": "string",
  "email": "string",
  "formule": "string",
  "commentaire": "string"
}

are appearing.

Result after adding deserialisation groups

It is the same for serialisation groups. See this picture Result after serialization groups We have

{
            "@id": "/api/pelerins/7",
            "@type": "Pelerin",
            "id": 7,
            "nom": "Knox",
            "prenom": "Tamara",
            "sexe": "M",
            "residence": "Chagai",
            "email": "nunc.id@esttempor.ca",
            "formule": "/api/formules/1",
            "commentaire": null
}

So I want to know What can I do to fix this error ? Thank

Advertisement

Answer

It’s solved. The problem is variables names. It seems that API Platform don’t recognize variables underscore in normalizationContext. look at here https://www.grafikart.fr/forum/topics/32395#p126203

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement