gpt4 book ai didi

php - Symfony - NelmioApiDocBundle : Show parameter description imported from class

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:49:28 26 4
gpt4 key购买 nike

我正在使用 NelmioApiDocBundle连同用于 REST API 的 PHP 框架 Symfony3。我想在/api/doc 页面中显示我的参数的描述。如果不手动添加参数,这可能吗?我想从输入/输出类中导入它。

这是我的文档的样子:

Screenshot

这是生成文档的 Controller 操作 (/api/user/login) 的 @ApiDoc:

 * @ApiDoc(
* section = "user",
* resource = true,
* description = "Checks the user credentials and returns an authentication & refresh token if they are correct",
* input = { "class" = "AppBundle\Libraries\Core\User\LoginRequest", "name" = "" },
* output = { "class" = "AppBundle\Libraries\Core\User\LoginResponse", "name" = "" },
* statusCodes = {
* 200 = "Returned when successful",
* 400 = "Returned when request syntax is incorrect",
* 404 = "Returned when the page is not found",
* 429 = "Returned when the client sent too many requests during a time period",
* 500 = "Returned when an internal server error occured",
* 501 = "Returned when an unavailable request method is user (GET, POST, DELETE, PUT, ...)",
* 503 = "Returned when the service is unavailable at the moment eg. due to maintenance or overload"
* },
*
* )

AppBundle\Libraries\Core\User\LoginRequest :

class LoginRequest implements JsonSerializable
{
/**
* The username.
*
* @var string
*
* @Assert\NotBlank()
* @Assert\Type("string")
*/
public $username;

/**
* The password.
*
* @var string
*
* @Assert\NotBlank()
* @Assert\Type("string")
*/
public $password;

/**
* Defines whether or not to save the refresh token as cooke.
*
* @var bool
*
* @Assert\NotBlank()
* @Assert\Type("bool")
*/
public $rememberPassword;

public function getUsername()
{
return $this->username;
}

public function setUsername($username)
{
$this->username = $username;
}

public function getPassword()
{
return $this->password;
}

public function setPassword($password)
{
$this->password = $password;
}

public function getRememberPassword()
{
return $this->rememberPassword;
}

public function setRememberPassword($rememberPassword)
{
$this->rememberPassword = $rememberPassword;
}

public function jsonSerialize()
{
return [
'username' => $this->username,
'password' => $this->password,
'rememberPassword' => $this->rememberPassword
];
}
}

我想使用这个类的描述,例如。对于用户名:“用户名。”,对于密码:“密码。”对于 rememberPassword:“定义是否将刷新 token 保存为 cooke。”。

感谢您的帮助。

问候语奥兰多

最佳答案

NelmioApiDoc 为以后生成的 View 提取数据的地方很少。但是您可以做的一件事是将您的描述添加到您的实体/模型类的表单类型中。

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('rememberPassword', CheckboxType::class, array(
'label' => 'input.remember.password',
// description will be passed to table in ApiDoc view
'description' => 'Defines whether or not to save the refresh token as cookie',
));
}

我知道您想知道是否有办法自动将更多信息添加到文档中,但只有几种方法。但是如果你想添加额外的信息,你可以通过评论来完成,如下例所示。

/**
* Lorem ipsum dolor sit amet
*
* #### Example of expected response ####
* [
* {
* "username": "Lorem ipsum dolor sit amet",
* "password": "Lorem ipsum dolor sit amet",
* "rememberPassword": {
* "1": "Lorem ipsum dolor sit amet",
* "2": "Lorem ipsum dolor sit amet",
* "3": "Lorem ipsum dolor sit amet"
* },
* },
* ...
* ]
*
* @ApiDoc(
* section = "user",
* resource = true,
* description = "Checks the user credentials and returns an authentication & refresh token if they are correct",
* input = { "class" = "AppBundle\Libraries\Core\User\LoginRequest", "name" = "" },
* output = { "class" = "AppBundle\Libraries\Core\User\LoginResponse", "name" = "" },
* statusCodes = {
* 200 = "Returned when successful",
* 400 = "Returned when request syntax is incorrect",
* 404 = "Returned when the page is not found",
* 429 = "Returned when the client sent too many requests during a time period",
* 500 = "Returned when an internal server error occured",
* 501 = "Returned when an unavailable request method is user (GET, POST, DELETE, PUT, ...)",
* 503 = "Returned when the service is unavailable at the moment eg. due to maintenance or overload"
* },
*
* )
*
*/
public function getLoginRequestAction()
{
// your code
}

关于php - Symfony - NelmioApiDocBundle : Show parameter description imported from class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38657150/

26 4 0