Skip to content
Advertisement

How to generate a GET request Swagger API Doc in Laravel 5.8 API?

I’m using L5-Swagger framework to generate an API Doc for my Laravel 5.8 project. Here is the code I have created so far:

 /**
 * @OAGET(
 *     path="/api/articles",
 *     tags={"articles"},
 *     summary="Returns articles with limit and offset",
 *     description="Description by Chanaka",
 *     operationId="sampleFunctionWithDoc",
 * )
 * @OAParameter(
 *    description="limit",
 *    in="path",
 *    name="limit",
 *    required=true,
 *    example="1",
 * )
 * @OAResponse(
 *    response="default",
 *    description="successful operation"
 * )
 * 
 */
public function showAllArticles($limit, $offset)
{
    return response()->json(Article::skip($offset)->take($limit)->get());
}

This is an endpoint with limit and offset. I added limit as a parameter here just to check. But it is not generating. Only the endpoint with path is available in the Swagger UI.

enter image description here

Please tell me what is the issue with my syntax. Is there another way to implement this.

Thank you in advance.

Advertisement

Answer

I believe annotations should be like this:

/**
 * @OAGET(
 *     path="/api/articles",
 *     tags={"articles"},
 *     summary="Returns articles with limit and offset",
 *     description="Description by Chanaka",
 *     operationId="sampleFunctionWithDoc",
 *     @OAParameter(
 *       description="limit",
 *       in="path",
 *       name="limit",
 *       required=true,
 *       example="1",
 *    ),
 *    @OAResponse(
 *       response="default",
 *       description="successful operation"
 *    )
 *  )
 * 
 */

So basically your parameters and response definitions should be another line in the root of the GET path “/api/articles” declaration.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement