Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
redha-ax2lan opened this issue Jul 6, 2020 · 19 comments
Closed

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

redha-ax2lan opened this issue Jul 6, 2020 · 19 comments

Comments

@redha-ax2lan
Copy link

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

@kelvin2200
Copy link

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

@redha-ax2lan
Copy link
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
Copy link

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
Copy link
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
Copy link

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
Copy link
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
Copy link
Author

this is my entity i don't have custom normalizer

@romainderocle
Copy link

did you find a solution ?

@redha-ax2lan
Copy link
Author

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

@dParadiz
Copy link

Try to run ./bin/console cache:clear

@redha-ax2lan
Copy link
Author

there is no cache that does not fix the bug

@TadjerouniMohamedAdel
Copy link

TadjerouniMohamedAdel commented Nov 1, 2020

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

@redha-ax2lan
Copy link
Author

after trying it works! thanks for the help !

@Tchekda
Copy link

Tchekda commented Nov 14, 2020

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

@angelomelonas
Copy link

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

@gediminasnn
Copy link

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

@gediminasnn
Copy link

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
Copy link

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
Copy link

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants