Symfony 5 api platform Groups() doesn’t work

With api platform, I wanted my User entity not to show my password with normalizationContext and groups but it didn’t show me anything…


/**
 * @ORMEntity(repositoryClass=UserRepository::class)
 * @ApiResource(
 *     normalizationContext={"groups"={"read"}}
 * )
 */
class User implements UserInterface
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     * Groups("read")
     */
    private $id;

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

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

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

    /**
     * @ORMColumn(type="string", length=255)
     * Groups("read")
     */
    private $firstname;

    /**
     * @ORMColumn(type="string", length=255)
     * Groups("read")
     */
    private $lastname;
...

He shows me that as a response:

{
  "@context": "/contexts/User",
  "@id": "/users",
  "@type": "hydra:Collection",
  "hydra:member": [
    {
      "@id": "/users/1",
      "@type": "User"
    },
    {
      "@id": "/users/2",
      "@type": "User"
    }
  ]
  }
}

How do we display all my properties except the password, I had succeeded in symfony 4 but in 5, it seems that it no longer works…

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

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