Reputation: 21
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 entity:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\Collection;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @ApiResource(
* collectionOperations={
* "post",
* "get"={
* "normalization_context"={"groups"={"user:read"}}
* }
* }
* )
*/
class User implements UserInterface
{
use ResourceId;
use Timestamble;
/**
* @ORM\Column(type="string", length=180, unique=true)
* @Groups("user:read")
*
*/
private $email;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;
/**
* @ORM\OneToMany(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 returns:
"@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"
},
// ...
],
"hydra:totalItems": 10
}
I don't understand why my emails are not visible, the collectionOperations
works, the problem comes to @Groups("user:read")
which isn't detected.
Upvotes: 0
Views: 1376
Reputation: 2689
just replace this
* @Groups("user:read")
by this:
* @Groups({"user:read"})
and don't forget to clear the cache each time you modify the metadata
Upvotes: 1