'Why my API-Plateform resource give me always and only 2 attributes in response?

I try to create a new API rest on monolithic project with symfony 4.4, api-platform 2.6.8, i have to use yaml configuration, but i have the same issue with annotation. One of my resource send 2 attributes (ID and label) when i try GET request on collection, whatever the configuration.

The entity :

 /**
 * Structure.
 *
 * @Gedmo\Tree(type="nested")
 * @ORM\Table(name="por_structure")
 * @ORM\Entity(repositoryClass="Common\StructureBundle\Repository\StructureRepository")
 * @UniqueEntity(fields={"parent", "code", "status"}, errorPath="code", message="common.structure.code.unique")
 * @UniqueEntity(fields={"parent", "label", "status"}, errorPath="label", message="common.structure.label.unique")
 */
class Structure
{
    /**
     * ID of the structure node.
     *
     * @var int
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * Label of the structure node.
     *
     * @var string
     * @Assert\NotBlank()
     * @ORM\Column(name="label", type="string", length=255, nullable=false)
     */
    private $label;

    /**
     * External ID of the structure node.
     *
     * @var string
     * @Assert\NotBlank()
     * @ORM\Column(name="code", type="string", length=255, nullable=false)
     */
    private $code;

    /**
     * Accounting code of the structure node.
     *
     * @var string
     * @ORM\Column(name="accounting_code", type="string", length=255, nullable=true)
     */
    private $accountingCode;

    /**
     * Status of the node (1: enabled, 0:disabled).
     *
     * @var bool
     * @ORM\Column(name="status", type="boolean")
     */
    private $status;

    /**
     * @Gedmo\TreeLeft
     * @ORM\Column(name="lft", type="integer")
     */
    private $lft;

    /**
     * @Gedmo\TreeLevel
     * @ORM\Column(name="lvl", type="integer")
     */
    private $lvl;

    /**
     * @Gedmo\TreeRight
     * @ORM\Column(name="rgt", type="integer")
     */
    private $rgt;

    /**
     * @var Structure
     * @Gedmo\TreeRoot
     * @ORM\ManyToOne(targetEntity="Common\StructureBundle\Entity\Structure")
     * @ORM\JoinColumn(name="root", referencedColumnName="id", onDelete="CASCADE")
     */
    private $root;
[...]

The resources.yaml :

resources:
  #Structure
  Common\StructureBundle\Entity\Structure:
    shortName: 'Common/Structure'
    properties:
      id:
        identifier: true
    collectionOperations:
      get:
        openapi_context:
          summary: 'Récupère une collection de Structure'
          description: 'Récupère une collection de Structure'
        normalization_context:
          groups: [ 'a2:read:Structure']
          openapi_definition_name: 'collection-read'

And the serializer.yaml (result is the same without any attribute in it):

Common\StructureBundle\Entity\Structure:
  attributes:
    id:
      groups: [ 'a2:read:Structure' ]
    label:
      groups: [ 'a2:read:Structure' ]
    code:
      groups: [ 'a2:read:Structure' ]
    status:
      groups: [ 'a2:read:Structure' ]

The GET answer is still :

{
  "@context": "\/api\/contexts\/Common\/Structure",
  "@id": "\/api\/v2\/common\/structures",
  "@type": "hydra:Collection",
  "hydra:member": [
    {
      "id": 1,
      "label": "SOCIETE 1 (FR)"
    },
    {
      "id": 2,
      "label": "Défaut"
    },
    {
      "id": 3,
      "label": "ZForfait jours"
    },
    [...]
    ],
  "hydra:totalItems": 42
}

What did i do wrong ?



Solution 1:[1]

OK i found the problem.

I work on a big application and somewhere i didn't know, there is a serialization process which used classes implementing NormalizerInterface.

in these classes there is the definition which locked the output.

I change NormalizerInterface, ContextAwareNormalizerInterface, and add something special in the context when thes are called by the other part of the app.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DarkChyper