模型示例未在 api 平台中正确显示

Model example not showing properly in api platform

我正在尝试从安全控制器记录自定义 API。但我注意到我的自定义 API 示例模型与其他模型有很大不同。以下是我的自定义 api 端点

的附件

这是我的用户实体,我尝试更改 swagger 上下文的参数,但似乎不起作用。

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

我想要这样的东西。

我怎样才能实现它?

你的响应定义中的 schema 也可以获得一个 example 键,与你的 User Login 参数完全相同(它不是 in 模式,而是兄弟姐妹)。即

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

正文参数注释无效。你需要更换

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

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


部分响应注解也无效,需要替换

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

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