简体   繁体   English

Symfony-NelmioApiDocBundle:显示从类导入的参数描述

[英]Symfony - NelmioApiDocBundle: Show parameter description imported from class

I am using the NelmioApiDocBundle together with the PHP framework Symfony3 for a REST API . 我将NelmioApiDocBundle和PHP框架Symfony3一起用于REST API I want to display the description of my parameters in the /api/doc page. 我想在/ api / doc页面中显示我的参数描述。 Is this possible without adding the parameters manually? 是否可以在不手动添加参数的情况下实现? I want to import it from the input/output class. 我想从输入/输出类导入它。

This is how my documentation looks: 这是我的文档的外观:

屏幕截图

Here ist my @ApiDoc of the controller action (/api/user/login) which generates the documentation: 我的@ApiDoc在这里生成了控制器文档(/ api / user / login):

 * @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 : 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
        ];
    }
}

I would like to use the desciptions of this class, eg. 我想使用此类的描述,例如。 for username: "The username.", for password: "The password." 用户名:“用户名”。密码:“密码”。 and for rememberPassword: "Defines whether or not to save the refresh token as cooke.". 并且要记住密码:“定义是否将刷新令牌保存为库克。”。

Thanks for the help. 谢谢您的帮助。

Greetings Orlando 问候奥兰多

There are only few places where NelmioApiDoc extracts the data for the later generated view. NelmioApiDoc仅在少数地方为以后生成的视图提取数据。 But one thing you can do is to add your descriptions to the form-type of your entity/model class. 但是您可以做的一件事就是将您的描述添加到实体/模型类的表单类型中。

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',
    ));
}

I know that you wanted to know if there are ways to put more informations to the documentation automatically, but there are only few. 我知道您想知道是否有办法自动将更多信息添加到文档中,但是只有少数几种。 But if you want to add additional informations you can do it through comments, like in the example below. 但是,如果您想添加其他信息,则可以通过注释来完成,例如下面的示例。

/**
 * 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM