Skip to content

normalization context don't work on symfony v5.1.99 #1576

@redha-ax2lan

Description

@redha-ax2lan

API Platform version(s) affected: x.y.z

Description

when i make my normalization context on symfony 5.1.99 in api ressource like that :
normalizationContext={"groups"={"read:question"}}
my model disapear in the api documentation and the api return 200 code but don't return element from database, only context from hydra

How to reproduce

normalizationContext={"groups"={"read:question"}} in api ressource and @groups in a entity.
and serializer: { enable_annotations: true } in config/package/framework.yamls

Possible Solution

i think it's cause it's a newest version.
Additional Context

this is worked on symfony 4.4
my php version is 7.3.14
api-platform-bug

Activity

kelvin2200

kelvin2200 commented on Jul 7, 2020

@kelvin2200

did you add the same normalization group to every property in your entity?

redha-ax2lan

redha-ax2lan commented on Jul 8, 2020

@redha-ax2lan
Author

I tested in both cases and it doesn't work, with all property in the same group and with 2 propertie in the group and here is only one group in this entity.

kelvin2200

kelvin2200 commented on Jul 8, 2020

@kelvin2200

from the little you provided, the route seems to be returning the expected result. Can you post the entire entity here? I think you are messing up somewhere with the denormalization groups.

redha-ax2lan

redha-ax2lan commented on Jul 9, 2020

@redha-ax2lan
Author

I do not have denormalization groups just a normalization, it is not necessary to create a group. my code worked on symfony 5.0 and 4.4

kelvin2200

kelvin2200 commented on Jul 10, 2020

@kelvin2200

well, if you don't want to post your entity, I don't know how to help. I'm running 5.1 and I have no issues. Do you have custom normalizers defined?

redha-ax2lan

redha-ax2lan commented on Jul 15, 2020

@redha-ax2lan
Author

`<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\QuestionRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**

  • @ApiResource(normalizationContext={"groups"={"read:quest"}}, collectionOperations={"get"}, itemOperations={"get"})

  • @Orm\Entity(repositoryClass="App\Repository\QuestionRepository")
    /
    class Question
    {
    /
    *

    • @Orm\Id()
    • @Orm\GeneratedValue()
    • @Orm\Column(type="integer")
    • @groups({"read:quest"})
      */
      private $id;

    /**

    • @Orm\Column(type="string", length=255)
    • @groups({"read:quest"})
      */
      private $libelle;

    /**

    • @Orm\OneToMany(targetEntity=Reponse::class, mappedBy="idQuestion", orphanRemoval=true)
    • @groups({"read:quest"})
      */
      private $reponses;

    public function __construct()
    {
    $this->reponses = new ArrayCollection();
    }

    public function getId(): ?int
    {
    return $this->id;
    }

    public function getLibelle(): ?string
    {
    return $this->libelle;
    }

    public function setLibelle(string $libelle): self
    {
    $this->libelle = $libelle;

     return $this;
    

    }

    /**

    • @return Collection|Reponse[]
      */
      public function getReponses(): Collection
      {
      return $this->reponses;
      }

    public function addReponse(Reponse $reponse): self
    {
    if (!$this->reponses->contains($reponse)) {
    $this->reponses[] = $reponse;
    $reponse->setIdQuestion($this);
    }

     return $this;
    

    }

    public function removeReponse(Reponse $reponse): self
    {
    if ($this->reponses->contains($reponse)) {
    $this->reponses->removeElement($reponse);
    // set the owning side to null (unless already changed)
    if ($reponse->getIdQuestion() === $this) {
    $reponse->setIdQuestion(null);
    }
    }

     return $this;
    

    }
    }
    `

redha-ax2lan

redha-ax2lan commented on Jul 15, 2020

@redha-ax2lan
Author

this is my entity i don't have custom normalizer

romainderocle

romainderocle commented on Oct 23, 2020

@romainderocle

did you find a solution ?

redha-ax2lan

redha-ax2lan commented on Oct 23, 2020

@redha-ax2lan
Author

not really, I downgraded symfony to 5.0 and it works, I haven't tried again since

dParadiz

dParadiz commented on Oct 27, 2020

@dParadiz

Try to run ./bin/console cache:clear

redha-ax2lan

redha-ax2lan commented on Oct 28, 2020

@redha-ax2lan
Author

there is no cache that does not fix the bug

TadjerouniMohamedAdel

TadjerouniMohamedAdel commented on Nov 1, 2020

@TadjerouniMohamedAdel

for me ./bin/console cache:clear solved the issue
symfony :5.1

redha-ax2lan

redha-ax2lan commented on Nov 2, 2020

@redha-ax2lan
Author

after trying it works! thanks for the help !

Tchekda

Tchekda commented on Nov 14, 2020

@Tchekda

Worked also for me !!
Is there a way to disable this cache in dev environment ?
Thanks

angelomelonas

angelomelonas commented on Dec 7, 2020

@angelomelonas

@dParadiz clearing the cache solved my problem after 3 hours of wondering why a simple get was not returning the entity data!

gediminasnn

gediminasnn commented on Jan 14, 2021

@gediminasnn

Same problem but with post operation, cache clearing does not fix it...

gediminasnn

gediminasnn commented on Jan 14, 2021

@gediminasnn

Same problem but with post operation, cache clearing does not fix it...

Bus somewhy if I add write group to setter method it magically works...

    /**
     * The description of the cheese as raw text.
     * @Groups({"write"})
     */
    public function setFirstName(string $first_name): self
    {
        $this->first_name = $first_name;

        return $this;
    }

NouraToukabriACF

NouraToukabriACF commented on Sep 24, 2021

@NouraToukabriACF

Same problem but with post operation, cache clearing does not fix it...

Bus somewhy if I add write group to setter method it magically works...

    /**
     * The description of the cheese as raw text.
     * @Groups({"write"})
     */
    public function setFirstName(string $first_name): self
    {
        $this->first_name = $first_name;

        return $this;
    }

Worked for me!! thank youu

abdojulari

abdojulari commented on Apr 7, 2022

@abdojulari

did you add the same normalization group to every property in your entity?

In my own case, I did, out of 5, only two appeared in the api documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @dParadiz@kelvin2200@abdojulari@angelomelonas@Tchekda

        Issue actions

          normalization context don't work on symfony v5.1.99 · Issue #1576 · api-platform/api-platform