In rest api GET request with optional parameters in URL and generate the swagger code in laravel

74 Views Asked by At

Laravel version = 9 Package = composer require "darkaonline/l5-swagger"

I have created a route like below

Route::GET('/contacts/{user_id?}/{site_id?}', [ContactController::class, 'getContacts']);

Which is working fine in POSTMAN and all.

Now i want to create the swagger API documentation for that. But unable to create.

/**
 * @OA\Get(
 *     path="/api/v1/contacts/{user_id?}/{site_id?}",
 *     operationId="getContacts",
 *     tags={"Contacts"},
 *     @OA\Parameter(
 *          name="user_id",
 *          in="path",
 *          required=false,
 *          allowEmptyValue=true,
 *          description="ID of the order that needs to be",
 *          @OA\Schema(
 *              type="integer",
 *              format="int"
 *          )
 *      ),
 *     @OA\Parameter(
 *           name="site_id",
 *           in="path",
 *           description="ID of the resource to retrieve",
 *           required=false,
 *           allowEmptyValue=true,
 *           @OA\Schema(
 *               type="integer",
 *               format="int"
 *           )
 *      ),
 *     @OA\Response(response="200", description="Success Entry",
 *            @OA\JsonContent()),
 *     @OA\Response(
 *           response=422,
 *           description="Unprocessable Entry",
 *           @OA\JsonContent()
 *       ),
 *     @OA\Response(
 *            response=400,
 *            description="Something went wrong",
 *            @OA\JsonContent()
 *        ),
 *    @OA\Response(
 *             response=404,
 *             description="Resource not found",
 *             @OA\JsonContent()
 *         ),
 * )
 */

The above code i have written but when clicking on execute from swagger below CURL call is showing. This is the code when i am not passing any parameter (The CURL)

curl -X 'GET' \
'http://localhost/project/api/v1/contacts/{user_id?%7D%2F%7Bsite_id=' \
-H 'accept: application/json' \
-H 'X-CSRF-TOKEN:

If i pass both my optional parameters then the result is showing.

Any help would be appreciated.

0

There are 0 best solutions below