如何在swagger php api注释中传递可选参数?

时间:2016-07-12 05:02:32

标签: php swagger swagger-ui swagger-2.0 swagger-php

  

我正在使用swagger ui,我有一个Get api,需要选择   参数,但我无法给下面的api注释   制作我尝试的代码:

/**
 * @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
});

但是当我试着这么夸张ui不带 id 可选参数时,它需要参数,帮帮我..

1 个答案:

答案 0 :(得分:2)

要定义可选参数,您只需将其定义为 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")
相关问题