API平台-序列化组无法正常工作

问题描述 投票:1回答:1

我有一个在Symfony 5和API平台下开发的项目。但是,我遇到一个非常奇怪的问题。当我向资源中添加序列化或反序列化组并尝试恢复集合甚至项目时,所有字段都不会出现在响应中。

这是我的资源定义

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints 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')"}
 *  }
 * )
 * @ORM\Entity(repositoryClass="App\Repository\PelerinRepository")
 */
class Pelerin
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"pelerins_read", "pelerins_read", "etat_sante_read"})
     */
    private $id;

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

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

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

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

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

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

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

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

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

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

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

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Formule", inversedBy="pelerins")
     * @ORM\JoinColumn(nullable=false)
     * @Groups({"pelerins_read"})
     * @Assert\NotBlank(message="Formule de voyage recquise pour continuer l'opération")
     */
    private $formule;

    /**
     * @ORM\Column(type="datetime")
     * @Groups({"pelerins_read"})
     * @Assert\NotBlank(message="La date d'inscription est requise")
     */
    private $date_inscription;

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

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

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

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

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

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

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User")
     * @ORM\JoinColumn(nullable=false)
     * @Groups({"pelerins_read"})
     */
    private $created_by;

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

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User")
     * @Groups({"pelerins_read"})
     */
    private $updated_by;

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

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

您只能看到此图片

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

正在出现。

Result after adding deserialisation groups

对于序列化组是相同的。看到这张图片Result after serialization groups我们有

{
            "@id": "/api/pelerins/7",
            "@type": "Pelerin",
            "id": 7,
            "nom": "Knox",
            "prenom": "Tamara",
            "sexe": "M",
            "residence": "Chagai",
            "email": "[email protected]",
            "formule": "/api/formules/1",
            "commentaire": null
}

所以我想知道如何解决此错误?谢谢

php api symfony serializer
1个回答
0
投票

已解决。问题是变量名。似乎API平台无法识别NormalizationContext中的下划线变量。看这里https://www.grafikart.fr/forum/topics/32395#p126203

© www.soinside.com 2019 - 2024. All rights reserved.