Symfony 模型示例未在api平台中正确显示
我正在尝试从安全控制器记录自定义API。但是我注意到我的定制API示例模型与其他模型有很大的不同。下面是从我的自定义api端点附加的 这是我的用户实体,我尝试过改变swagger上下文的参数,但似乎不起作用Symfony 模型示例未在api平台中正确显示,symfony,swagger,symfony4,api-platform.com,Symfony,Swagger,Symfony4,Api Platform.com,我正在尝试从安全控制器记录自定义API。但是我注意到我的定制API示例模型与其他模型有很大的不同。下面是从我的自定义api端点附加的 这是我的用户实体,我尝试过改变swagger上下文的参数,但似乎不起作用 <?php /** * @ApiResource( * collectionOperations={ * "get", * "post" ={ * "route_name"="api_users_post_collecti
<?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;
}
}
id;
}
公共函数getEmail():?字符串
{
返回$this->email;
}
公共函数setEmail(字符串$email):self
{
$this->email=$email;
退还$this;
}
/**
*表示此用户的可视标识符。
*
*@见用户界面
*/
公共函数getUsername():字符串
{
返回(字符串)$this->email;
}
/**
*@见用户界面
*/
公共函数getRoles():数组
{
$roles=$this->roles;
//确保每个用户至少具有角色\u user
$roles[]='ROLE_USER';
返回数组_unique($roles);
}
公共函数setRoles(数组$roles):self
{
$this->roles=$roles;
退还$this;
}
/**
*@见用户界面
*/
公共函数getPassword():字符串
{
返回(字符串)$this->password;
}
公共函数setPassword(字符串$password):self
{
$this->password=$password;
退还$this;
}
/**
*@见用户界面
*/
公共函数getSalt()
{
//在security.yaml中使用“bcrypt”算法时不需要
}
/**
*@见用户界面
*/
公共职能部门()
{
//如果在用户上存储任何临时敏感数据,请在此处清除
//$this->plainPassword=null;
}
公共函数getFirstName():?字符串
{
返回$this->firstName;
}
公共函数setFirstName(字符串$firstName):self
{
$this->firstName=$firstName;
退还$this;
}
公共函数getLastName():?字符串
{
返回$this->lastName;
}
公共函数setLastName(字符串$lastName):self
{
$this->lastName=$lastName;
退还$this;
}
公共函数getDob():?\DateTimeInterface
{
返回$this->dob;
}
公共函数setDob(\DateTimeInterface$dob):self
{
$this->dob=$dob;
退还$this;
}
公共函数getAddress():?字符串
{
返回$this->address;
}
公共函数setAddress(字符串$address):self
{
$this->address=$address;
退还$this;
}
公共函数getContact():?字符串
{
返回$this->contact;
}
公共函数setContact(字符串$contact):self
{
$this->contact=$contact;
退还$this;
}
}
我想要这样的东西
如何实现它?响应定义中的
模式
也可以获得一个示例
键,与用户登录
参数完全相同(该参数不在模式中,而是同级)。i、 e
正文参数批注无效。你需要更换
* {
* "name" = "User Login",
* "in" = "body",
* "type" = "object",
* "schema"= {
* "email" = {"type": "string"},
* "password" = {"type" : "string"}
* },
* "example" ={
* "email" = "string",
* "password" ="string"
*
* }
* "required" = {
* "email",
* "password"
* },
与
一些响应注释也无效,需要替换
* {
* "name" = "User Login",
* "in" = "body",
* "type" = "object",
* "schema"= {
* "email" = {"type": "string"},
* "password" = {"type" : "string"}
* },
* "example" ={
* "email" = "string",
* "password" ="string"
*
* }
* "required" = {
* "email",
* "password"
* },
与
虽然这个答案在某种程度上是正确的(因为模式确实支持自定义
示例
),但OP的问题实际上是由不正确的注释引起的。@Helen对此感到疑惑,但不确定我以前没有使用过的@ApiResource
注释。但既然是大摇大摆。。。您的回答非常具体,只针对请求,而不是响应(OP主要问题)。
* "required" = {
* "token"
* },