我的模型如下所示:
use Symfony\Component\Serializer\Annotation\Groups;
use OpenApi\Annotations as OA;
class ResponseBodyModel extends AbstractModel
{
/**
* @var AbstractModel
* @Groups({"comment_list", "comment_create"})
*/
private AbstractModel $data;
public function setData( AbstractModel $data ): self
{
$this->data = $data;
return $this;
}
public function getData(): AbstractModel
{
return $this->data;
}
//+ some other parameters
}
class CommentModel extends AbstractModel {
//parameters for comments (ID, content, userModel, etc.)
//with @Groups({"comment_create","comment_list"})
}
class CollectionModel extends AbstractModel {
/**
* @var CommentModel[]
* @Groups ({"comment_list"})
*/
private array $comments = [];
}
控制器中的代码:
use OpenApi\Annotations as OA;
class DefaultController
{
/**
* @Route("/comment", methods={"POST"})
* @OA\Response (
* response=200,
* description="Return newly added comment",
* @OA\JsonContent(
* ref=@Model(type=ResponseBodyModel::class, groups={"comment_create"})
* )
* )
*/
public function create()
{
$commentModel = new CommentModel();
$this->responseBodyModelService->initialize($commentModel);
}
/**
* @Route("/comments", methods={"GET"})
* @OA\Response (
* response=200,
* description="Return list of comments",
* @OA\JsonContent(
* ref=@Model(type=ResponseBodyModel::class, groups={"comment_list"})
* )
* )
*/
public function list()
{
$collectionModel = new CollectionModel();
$collectionModel->initialize($arrayOfCommentsModels);
$this->responseBodyModelService->initialize($collectionModel);
}
}
在这种情况下,NelmioApiDocBundle 将生成
{
"data": {},
"status": "string"
}
因为注释ResponseBodyModel::$data
是@var AbstractModel
(我知道这一点)
如何为参数编写注释以ResponseBodyModel::$data
根据集合组选择正确的类模型:
- 当我设置@group
comment_create
- 选择CommentModel::class
- 当我设置@gropu
comment_list
- 选择CollectionModel::class
在 NelmioApiBundle 的文档中与此无关
https://symfony.com/doc/4.x/bundles/NelmioApiDocBundle/index.html
我也找不到任何关于这个的东西https://swagger.io/specification/
。