title: Unique Constraint return type mismatch
body: <p>I have this User entity.</p>
  
  <pre><code>&lt;?php
  
  namespace App\Entity;
  
  use ApiPlatform\Core\Annotation\ApiResource;
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  use Symfony\Component\Security\Core\User\UserInterface;
  use Symfony\Component\Validator\Constraints as Assert;
  use Symfony\Component\Serializer\Annotation\Groups;
  
  /**
   * @ApiResource(
   *     collectionOperations={
   *          "register"={
   *              "method"="POST",
   *              "path"="/users/register",
   *              "denormalization_context"={"groups"={"register"}},
   *              "normalization_context"={"groups"={"read"}},
   *              "validation_groups"={"register"},
   *              "swagger_context"={
   *                  "summary"="Register a user",
   *                  "description"="For anonymous user to register an account."
   *              }
   *          }
   *     }
   * )
   * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
   */
  class User implements UserInterface
  {
      const ROLE_USER = 'ROLE_USER';
      const ROLE_WEBADMIN = 'ROLE_WEBADMIN';
      const ROLE_SUPERADMIN = 'ROLE_SUPERADMIN';
  
      const DEFAULT_ROLES = [self::ROLE_USER];
  
      /**
       * @ORM\Id()
       * @ORM\GeneratedValue()
       * @ORM\Column(type="integer")
       * @Groups({"read"})
       */
      private $id;
  
      /**
       * @Assert\Unique(groups={"write","register"})
       * @ORM\Column(type="string", length=180, unique=true)
       * @Assert\Length(min="6", max="255")
       * @Groups({"read", "write", "register"})
       */
      private $username;
  
      /**
       * @Assert\Unique(groups={"write","register"})
       * @ORM\Column(type="string", length=255, unique=true)
       * @Assert\Email(groups={"write","register"})
       * @Assert\NotBlank(groups={"register"})
       * @Assert\Length(min=6, max=255, groups={"write","register"})
       * @Groups({"write","register"})
       */
      private $email;
  
      /**
       * @ORM\Column(type="json")
       * @Groups({"read"})
       */
      private $roles = [];
  
      /**
       * @var string The hashed password
       * @ORM\Column(type="string")
       * @Assert\NotBlank(groups={"write", "register"})
       * @Assert\Regex(
       *     pattern="/(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).{7,}/",
       *     message="Password must be minimum seven characters long and contain at least one digit, one upper case letter, and one lower case letter"
       * )
       * @Groups({"write", "register"})
       */
      private $password;
  
      /**
       * @Groups({"register"})
       * @Assert\NotBlank(groups={"register"})
       * @Assert\Expression(
       *     "this.getPassword() === this.getRetypePassword()",
       *     message="Passwords does not match",
       *     groups={"register"}
       * )
       */
      private $retypePassword;
  
      /**
       * @ORM\Column(name="password_change_date", type="integer", nullable=true)
       */
      private $passwordChangeDate;
  
      /**
       * @var string Full name
       * @ORM\Column(type="string", length=255)
       * @Assert\NotBlank()
       * @Groups({"read", "write", "register"})
       */
      private $name;
  
      /**
       * @ORM\Column(name="is_active", type="boolean", options={"default": 0} )
       * @Groups({"read"})
       */
      private $isActive;
  
      /**
       * @ORM\Column(name="confirmation_token", type="string", length=40, nullable=true)
       */
      private $confirmationToken;
  
      /**
       * User constructor.
       */
      public function __construct()
      {
          $this-&gt;setIsActive(false)
              -&gt;setConfirmationToken(NULL)
              -&gt;setRoles(self::DEFAULT_ROLES);
      }
  
      public function getId(): ?int
      {
          return $this-&gt;id;
      }
  
      /**
       * A visual identifier that represents this user.
       *
       * @see UserInterface
       */
      public function getUsername(): string
      {
          return (string) $this-&gt;username;
      }
  
      public function setUsername(string $username): self
      {
          $this-&gt;username = $username;
  
          return $this;
      }
  
      /**
       * @see UserInterface
       */
      public function getRoles(): array
      {
          $roles = $this-&gt;roles;
          // guarantee every user at least has ROLE_USER
          $roles[] = 'ROLE_USER';
  
          return array_unique($roles);
      }
  
      public function setRoles(array $roles): self
      {
          $this-&gt;roles = $roles;
  
          return $this;
      }
  
      /**
       * @see UserInterface
       */
      public function getPassword(): string
      {
          return (string) $this-&gt;password;
      }
  
      public function setPassword(string $password): self
      {
          $this-&gt;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-&gt;plainPassword = null;
      }
  
      public function getEmail(): ?string
      {
          return $this-&gt;email;
      }
  
      public function setEmail(string $email): self
      {
          $this-&gt;email = $email;
  
          return $this;
      }
  
      public function getName(): ?string
      {
          return $this-&gt;name;
      }
  
      public function setName(string $name): self
      {
          $this-&gt;name = $name;
  
          return $this;
      }
  
      public function getIsActive(): ?bool
      {
          return $this-&gt;isActive;
      }
  
      public function setIsActive(bool $isActive): self
      {
          $this-&gt;isActive = $isActive;
  
          return $this;
      }
  
      public function getConfirmationToken(): ?string
      {
          return $this-&gt;confirmationToken;
      }
  
      public function setConfirmationToken(?string $confirmationToken): self
      {
          $this-&gt;confirmationToken = $confirmationToken;
  
          return $this;
      }
  
      /**
       * @return mixed
       */
      public function getPasswordChangeDate()
      {
          return $this-&gt;passwordChangeDate;
      }
  
      /**
       * @param mixed $passwordChangeDate
       */
      public function setPasswordChangeDate($passwordChangeDate): void
      {
          $this-&gt;passwordChangeDate = $passwordChangeDate;
      }
  
      /**
       * @return string
       */
      public function getRetypePassword(): string
      {
          return $this-&gt;retypePassword;
      }
  
      /**
       * @param string $retypePassword
       */
      public function setRetypePassword(string $retypePassword): void
      {
          $this-&gt;retypePassword = $retypePassword;
      }
  }
  
  </code></pre>
  
  <p>When I request (via Insomnia, postman-like api tools, headers already set)</p>
  
  <pre><code>{
    "username": "cerseilann",
    "password": "password",
    "email": "cerseilann@example.com",
    "name": "Cersei Lannister Duplicate",
    "retypePassword": "password"
  }
  </code></pre>
  
  <p>I'm expecting some error message related with unique value. But instead, I receive this error: </p>
  
  <p><code>This value should be of type array|IteratorAggregate</code></p>
  
  <p>Full response</p>
  
  <pre><code>{
    "@context": "\/api\/contexts\/ConstraintViolationList",
    "@type": "ConstraintViolationList",
    "hydra:title": "An error occurred",
    "hydra:description": "username: This value should be of type array|IteratorAggregate.\nemail: This value should be of type array|IteratorAggregate.",
    "violations": [
      {
        "propertyPath": "username",
        "message": "This value should be of type array|IteratorAggregate."
      },
      {
        "propertyPath": "email",
        "message": "This value should be of type array|IteratorAggregate."
      }
    ]
  }
  </code></pre>
  
  <p>What did I miss?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>When I use UniqueEntity and remove the <code>Unique</code></p>
  
  <pre><code>...
   * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
   * @UniqueEntity(fields="username", message="Username {{ value }} already taken. Please use another username.")
   * @UniqueEntity(fields="email", message="Email {{ value }} already being used. COnsider forgot password to retrieve the password.")
   */
  
  ...
  </code></pre>
  
  <p>I got this return error</p>
  
  <pre><code>{
    "@context": "\/api\/contexts\/Error",
    "@type": "hydra:Error",
    "hydra:title": "An error occurred",
    "hydra:description": "An exception occurred while executing 'INSERT INTO user (username, email, roles, password, password_change_date, name, is_active, confirmation_token) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' with params [\"cerseilann\", \"cerseilann1@example.com\", \"[\\\u0022ROLE_USER\\\u0022]\", \"$argon2id$v=19$m=65536,t=4,p=1$0Wu+2mjS\\\/Uft6lu2ieJOMQ$6ostmakEH2LKYKVvr5PYCoFOZvDwwNT\\\/\\\/+xI+g9hkG8\", null, \"Cersei Lannister 1\", 0, \"adde39972ac50feb3918\"]:\n\nSQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'cerseilann' for key 'UNIQ_8D93D649F85E0677'",
    "trace": [
  
  ...
  </code></pre>
  
  <p>I'm expecting the error message to be appear.</p>