Achilles
Achilles

Reputation: 441

Model example not showing properly in api platform

I am trying to document Custom API from the Security Controller. But I notice that my custom API example model is quite different from other models. Below are the attached from my custom api endpoint

enter image description here

enter image description here

Here is my user entity, I have tried changing parameters of swagger context but seems like it is not working.

<?php

/**
 * @ApiResource(
 *     collectionOperations={
 *       "get",
 *       "post" ={
 *         "route_name"="api_users_post_collection"
 *        },
 *          "app_login"={
 *              "route_name"="app_login",
 *              "method"="POST",
 *               "swagger_context" = {
 *                  "parameters" = {
 *                      {
 *                          "name" = "User Login",
 *                          "in" = "body",
 *                          "type" = "object",
 *                          "schema"= {
 *                                   "email" = {"type": "string"},
 *                                   "password" = {"type" : "string"}
 *                          },
 *                          "example" ={
 *                                  "email" = "string",
 *                                  "password" ="string"
 *
 *                          }
 *                      }
 *                  },
 *                  "responses" = {
 *                      "200" = {
 *                          "description" = "You will get generate token",
 *                          "schema" =  {
 *                              "type" = "object",
 *                              "required" = {
 *                                  "email",
 *                                  "password"
 *                              },
 *                              "properties" = {
 *                                   "token" = {
 *                                      "type" = "string"
 *                                   }
 *                              }
 *                          }
 *                      },
 *                      "400" = {
 *                          "description" = "Invalid input"
 *                      }
 *                  },
 *                  "summary" = "Get Token (Login)",
 *                  "description" = "Get user token by email and password",
 *                  "consumes" = {
 *                      "application/json",
 *                      "text/html",
 *                   },
 *                  "produces" = {
 *                      "application/json",
 *                      "application/ld+json"
 *                   }
 *              }
 *          }
 *     },
 *     itemOperations={
 *              "get",
 *              "put"
 *     },
 *     normalizationContext={
 *                  "groups"={"user:read"},"swagger_definition_name"="Read"
 *      },
 *     denormalizationContext={
 *                  "groups"={"user:write"},"swagger_definition_name"="Write"
 *      },
 *     shortName="User"
 *
 * )
 * @UniqueEntity(fields={"email"})
 * @UniqueEntity(fields={"contact"})
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 */
class User implements UserInterface
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=180, unique=true)
     * @Groups({"user:read", "user:write"})
     * @Assert\Email()
     * @Assert\NotBlank()
     */
    private $email;

    /**
     * @ORM\Column(type="json")
     */
    private $roles = [];

    /**
     * @var string The hashed password
     * @ORM\Column(type="string")
     * @Groups({"user:write"})
     * @Assert\NotBlank()
     */
    private $password;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"user:read", "user:write"})
     * @Assert\NotBlank()
     */
    private $firstName;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"user:read", "user:write"})
     * @Assert\NotBlank()
     */
    private $lastName;

    /**
     * @var string provide in YYYY-MM-DD (neglect Time)
     * @ORM\Column(type="date")
     * @Groups({"user:read", "user:write"})
     * @Assert\NotBlank()
     */
    private $dob;

    /**
     * @ORM\Column(type="text")
     * @Groups({"user:read", "user:write"})
     * @Assert\NotBlank()
     */
    private $address;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"user:read", "user:write"})
     * @Assert\NotBlank()
     * @Assert\Length(
     *     min=8,
     *     max=8,
     *     maxMessage="contact number must have 8 character",
     *     minMessage="contact number must have 8 character"
     * )
     */
    private $contact;

    public function getId(): ?int
    {
        return $this->id;
    }

    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;
    }

    public function getFirstName(): ?string
    {
        return $this->firstName;
    }

    public function setFirstName(string $firstName): self
    {
        $this->firstName = $firstName;

        return $this;
    }

    public function getLastName(): ?string
    {
        return $this->lastName;
    }

    public function setLastName(string $lastName): self
    {
        $this->lastName = $lastName;

        return $this;
    }

    public function getDob(): ?\DateTimeInterface
    {
        return $this->dob;
    }

    public function setDob(\DateTimeInterface $dob): self
    {
        $this->dob = $dob;

        return $this;
    }

    public function getAddress(): ?string
    {
        return $this->address;
    }

    public function setAddress(string $address): self
    {
        $this->address = $address;

        return $this;
    }

    public function getContact(): ?string
    {
        return $this->contact;
    }

    public function setContact(string $contact): self
    {
        $this->contact = $contact;

        return $this;
    }
}

I want to have something like this.

enter image description here How can I achieve it?

Upvotes: 2

Views: 2690

Answers (2)

Helen
Helen

Reputation: 98022

The body parameter annotations are not valid. You need to replace

 *                      {
 *                          "name" = "User Login",
 *                          "in" = "body",
 *                          "type" = "object",
 *                          "schema"= {
 *                                   "email" = {"type": "string"},
 *                                   "password" = {"type" : "string"}
 *                          },
 *                          "example" ={
 *                                  "email" = "string",
 *                                  "password" ="string"
 *
 *                          }

with

 *                      {
 *                          "name" = "User Login",
 *                          "in" = "body",
 *                          "required" = true,
 *                          "schema"= {
 *                                   "type" = "object",
 *                                   "required" = {
 *                                            "email",
 *                                            "password"
 *                                   },
 *                                   "properties" = {
 *                                            "email" = {"type": "string"},
 *                                            "password" = {"type" : "string"}
 *                                   }
 *                          }


Some of the response annotations are also not valid, you need to replace

 *                              "required" = {
 *                                  "email",
 *                                  "password"
 *                              },

with

 *                              "required" = {
 *                                  "token"
 *                              },

Upvotes: 2

Jakumi
Jakumi

Reputation: 8374

the schema in your response-definition can also get an example key, exactly the same as your User Login parameter (where it's not in the schema, but a sibling instead). i.e.

"example" = {"email": "string", "password":"string", ...}

Upvotes: 1

Related Questions