Groups annotation doesn’t work with api-plateform when i want to serialize

I can’t use group annotation to get only values who I decide to show. In my exemple, I want to get only emails of my users when I show all users with collectionOperations. It’s the same problem with itemOperations

Here is my symfony entity


namespace AppEntity;

use DoctrineORMMapping as ORM;
use SymfonyComponentSerializerAnnotationGroups;
use AppRepositoryUserRepository;
use DoctrineCommonCollectionsCollection;
use ApiPlatformCoreAnnotationApiResource;
use DoctrineCommonCollectionsArrayCollection;
use SymfonyComponentSecurityCoreUserUserInterface;

/**
 * @ORMEntity(repositoryClass=UserRepository::class)
 * @ApiResource(
 *     collectionOperations={
 *         "post",
 *         "get"={
 *             "normalization_context"={"groups"={"user:read"}}
 *         }
 *     }
 * )
 */
class User implements UserInterface
{
    use ResourceId;
    use Timestamble;

    /**
     * @ORMColumn(type="string", length=180, unique=true)
     * @Groups("user:read")
     * 
     */
    private $email;

    /**
     * @ORMColumn(type="json")
     */
    private $roles = [];

    /**
     * @var string The hashed password
     * @ORMColumn(type="string")
     */
    private $password;

    /**
     * @ORMOneToMany(targetEntity=Article::class, mappedBy="author", orphanRemoval=true)
     */
    private $articles;

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

        $this->createdAt = new DateTimeImmutable();
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    /**
     * A visual identifier that represents this user.
     *
     * @see UserInterface
     */
    public function getUsername(): string
    {
        return (string) $this->email;
    }

    /**
     * @see UserInterface
     */
    public function getRoles(): array
    {
        $roles = $this->roles;
        // guarantee every user at least has ROLE_USER
        $roles[] = 'ROLE_USER';

        return array_unique($roles);
    }

    public function setRoles(array $roles): self
    {
        $this->roles = $roles;

        return $this;
    }

    /**
     * @see UserInterface
     */
    public function getPassword(): string
    {
        return (string) $this->password;
    }

    public function setPassword(string $password): self
    {
        $this->password = $password;

        return $this;
    }

    /**
     * @see UserInterface
     */
    public function getSalt()
    {
        // not needed when using the "bcrypt" algorithm in security.yaml
    }

    /**
     * @see UserInterface
     */
    public function eraseCredentials()
    {
        // If you store any temporary, sensitive data on the user, clear it here
        // $this->plainPassword = null;
    }

    /**
     * @return Collection|Article[]
     */
    public function getArticles(): Collection
    {
        return $this->articles;
    }

    public function addArticle(Article $article): self
    {
        if (!$this->articles->contains($article)) {
            $this->articles[] = $article;
            $article->setAuthor($this);
        }

        return $this;
    }

    public function removeArticle(Article $article): self
    {
        if ($this->articles->contains($article)) {
            $this->articles->removeElement($article);
            // set the owning side to null (unless already changed)
            if ($article->getAuthor() === $this) {
                $article->setAuthor(null);
            }
        }

        return $this;
    }
}

My api should return only email when i get in all my users, but return:

  "@context": "/api/contexts/User",
  "@id": "/api/users",
  "@type": "hydra:Collection",
  "hydra:member": [
    {
      "@id": "/api/users/1",
      "@type": "User"
    },
    {
      "@id": "/api/users/2",
      "@type": "User"
    },
    {
      "@id": "/api/users/3",
      "@type": "User"
    },
    {
      "@id": "/api/users/4",
      "@type": "User"
    },
    {
      "@id": "/api/users/5",
      "@type": "User"
    },
    {
      "@id": "/api/users/6",
      "@type": "User"
    },
    {
      "@id": "/api/users/7",
      "@type": "User"
    },
    {
      "@id": "/api/users/8",
      "@type": "User"
    },
    {
      "@id": "/api/users/9",
      "@type": "User"
    },
    {
      "@id": "/api/users/10",
      "@type": "User"
    }
  ],
  "hydra:totalItems": 10
}

i don’t understand why my emails are not visible, the collectionOperations works, the problem comes to @Groups("user:read") who isn’t detected …

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *