1

我正在使用 swagger ui,我有一个 Get api,它采用可选参数,但我无法为该 api 提供注释,下面是我尝试过的 make 代码:

/**
 * @SWG\Get(
 *     path="/basics/checkDataNameAvailability/{type}/{name}{/id}",
 *     tags={"Emergency"},
 *     description="Return data useful for userGroup, deviceGroups and deviceTags",
 *     produces={"application/json", "application/xml", "text/xml", "text/html"},
 *     @SWG\Parameter(name="type",in="path",description="return useful data by type",required=true,type="integer", @SWG\Items(type="integer"),collectionFormat="csv",format="int32"),
 *     @SWG\Parameter(name="name",in="path",description="return useful data by name",required=true,type="integer", @SWG\Items(type="integer"),collectionFormat="csv",format="int32"),
 *     @SWG\Parameter(name="id",in="path",description="optional, useful data by id",required=true,type="integer", @SWG\Items(type="integer"),collectionFormat="csv",format="int32"),
 *     @SWG\Response(response=200,description="Dashboard Response",
 *          @SWG\Schema(type="array",@SWG\Items(ref="#/definitions/Pet"))
 *     ),
 *     @SWG\Response(response="default",description="unexpected error",
 *          @SWG\Schema(ref="#/definitions/ErrorModel")
 *     ),
 *     @SWG\ExternalDocumentation(description="find more info here", url="https://swagger.io/about")
 * )
 */ 

我的 Get api 结构是这样的:

$app->get('/checkDataNameAvailability/:type/:name(/:id)', function($type, $name, $id = '') use($app){
//here is my api code
});

但是当我尝试这样做时,swagger ui 不采用id可选参数,它采用必需参数,帮助我..

4

1 回答 1

3

要定义可选参数,您只需不将其定义为NOT required

id参数required=true有:

@SWG\Parameter(name="id",in="path",description="optional, useful data by id",required=true,type="integer", @SWG\Items(type="integer"),collectionFormat="csv",format="int32")

您只需将其设置为 false:

@SWG\Parameter(name="id",in="path",description="optional, useful data by id",required=false,type="integer", @SWG\Items(type="integer"),collectionFormat="csv",format="int32")

删除required=true也会使这个参数成为可选的

@SWG\Parameter(name="id",in="path",description="optional, useful data by id",type="integer", @SWG\Items(type="integer"),collectionFormat="csv",format="int32")
于 2016-07-12T19:28:41.940 回答