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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)