简体   繁体   中英

how to pass optional parameter in swagger php api annotation?

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..

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=true also 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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM