我有一个用Symfony 3.4编写的应用程序,该应用程序使用FOSRestBundle
公开JSON控制器,并且试图使用NelmioApiDocBundle
来记录我的API。
这是我第一次使用NelmioApiDocBundle
,其文档分散在许多其他项目中。
每个控制器都返回相同的响应。这是成功响应的示例:
{
"status": "success",
"data": {},
"code": null,
"message": null
}
这是错误响应的示例:
{
"status": "error",
"data": null,
"code": 201,
"message": "User not found."
}
这是另一个:
{
"status": "error",
"data": null,
"code": 202,
"message": "Invalid password."
}
如您所见,我只是使用错误/成功来修改状态,并且提供了用于识别错误的数字代码。
我该如何使用Swagger注释来描述它,以尽可能多地回收代码?
/**
* @Route("/api/users")
*/
class UserController extends AbstractFOSRestController
{
/**
* User login.
*
* @Rest\Post("/login")
* @SWG\Response(
* response=400,
* description="Username or password fields not found",
* @SWG\Schema(type="object",
* @SWG\Property(property="status", type="string"), <=== how can I say that it's error?
* @SWG\Property(property="data", type="object"),
* @SWG\Property(property="code", type="null"), <=== how can I say that it's 201?
* @SWG\Property(property="message", type="string"),
* )
*
* )
* @SWG\Response(
* response=200,
* description="Returns the user and the authentication token",
* @SWG\Schema(type="object",
* @SWG\Property(property="status", type="string"), <=== how can I say that it's success?
* @SWG\Property(property="data", type="object"),
* @SWG\Property(property="code", type="null"),
* @SWG\Property(property="message", type="string"),
* )
* )
* @SWG\Parameter(
* name="body",
* in="body",
* @SWG\Schema(type="object", required={"username"},
* @SWG\Property(property="username", type="string", example="admin"),
* @SWG\Property(property="password", type="string", example="admin")
* )
* )
* @SWG\Tag(name="users")
* @Security(name="api_key")
*/
public function loginAction(Request $request, UserPasswordEncoderInterface $encoder)
{
}
}