dousou2897 2016-07-12 05:02
浏览 632
已采纳

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

I am using swagger ui and i have a Get api which take take optional parameter but i am unable to give annotation for that api below is make code which i have tried:

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

and my Get api structure is like:

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

But when i am trying this so swagger ui does not take id optional parameter, it take required parameter, help me..

  • 写回答

1条回答 默认 最新

  • dongzhentiao2326 2016-07-12 19:28
    关注

    To define an optional parameter, you just need to not define it as NOT required.

    The id parameter has 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")
    

    You just need to set it to 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")
    

    Removing required=truealso make this parameter optional

    @SWG\Parameter(name="id",in="path",description="optional, useful data by id",type="integer", @SWG\Items(type="integer"),collectionFormat="csv",format="int32")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记