首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模型示例未在api平台中正确显示

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

Stack Overflow用户
提问于 2019-08-11 08:43:36
回答 2查看 2K关注 0票数 2

我试图记录来自安全控制器的自定义API。但我注意到,我的自定义API示例模型与其他模型有很大不同。下面是来自我的自定义api端点的附件

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

代码语言:javascript
复制
<?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;
    }
}

我想要这样的东西。

我怎样才能做到这一点?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-08-12 09:10:52

主体参数注释无效。你需要替换

代码语言:javascript
复制
 *                      {
 *                          "name" = "User Login",
 *                          "in" = "body",
 *                          "type" = "object",
 *                          "schema"= {
 *                                   "email" = {"type": "string"},
 *                                   "password" = {"type" : "string"}
 *                          },
 *                          "example" ={
 *                                  "email" = "string",
 *                                  "password" ="string"
 *
 *                          }

使用

代码语言:javascript
复制
 *                      {
 *                          "name" = "User Login",
 *                          "in" = "body",
 *                          "required" = true,
 *                          "schema"= {
 *                                   "type" = "object",
 *                                   "required" = {
 *                                            "email",
 *                                            "password"
 *                                   },
 *                                   "properties" = {
 *                                            "email" = {"type": "string"},
 *                                            "password" = {"type" : "string"}
 *                                   }
 *                          }

一些响应注释也无效,您需要替换

代码语言:javascript
复制
 *                              "required" = {
 *                                  "email",
 *                                  "password"
 *                              },

使用

代码语言:javascript
复制
 *                              "required" = {
 *                                  "token"
 *                              },
票数 2
EN

Stack Overflow用户

发布于 2019-08-11 09:52:24

您的响应定义中的schema也可以得到一个example键,与您的User Login参数完全相同(在那里它不在模式中,而是在同级中)。即

代码语言:javascript
复制
"example" = {"email": "string", "password":"string", ...}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57448554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档