L5-swagger api documentation: Getting error Required @OA\PathItem() not found

Viewed 2878

After reading these 2 posts here on Stackoverflow: How to Solved ErrorException : Required @OA\PathItem() not found Can't generate API documentation in l5-swagger

I still get an error Required @OA\PathItem() not found after running php artisan l5-swagger:generate.

This is my Controller.php part:

/**
 * @OA\Info(
 *     title="My First API Documentation",
 *     version="0.1",
 *      @OA\Contact(
 *          email="info@yeagger.com"
 *      ),
 * ),
 *  @OA\Server(
 *      description="Learning env",
 *      url="https://foo.localhost:8000/api/"
 *  ),
 */
class Controller extends BaseController
{

and this is my ProfileController part:

   /**
     * @OA\Get(
     *      path="/profiles",
     *      @OA\Response(
     *          response=200,
     *          description="Successful operation",
     *      ),
     *     @OA\PathItem (
     *     ),
     * )
     */
   function index()
    {
        return new ProfileCollection(Profile::with('user')->paginate());
    }

What am I overlooking here? If anyone can explain and help that would be great :)

EDIT - SOLUTION

The problem occured because I am using a laravel modules package and I had to change a bit of code in the l5-swagger.php config file:

'annotations' => [
                    base_path('Modules/Api/Http'), <-- changed the base path to the correct module
                ],

I then copied the main Controller.php from App/Http/Controllers to the same Module to also get rid of the occuring @OA\Info() not found error after that.

2 Answers

When I first installed and configured I was having the same error. Turns out that just @OA\Info is not enough to generate the document. In addition to that, it requires atleast one path entry. After adding an api endpoint annotation it got fixed.

example:

/**
 * @OA\Get(
 *     path="/api/users",
 *     @OA\Response(response="200", description="An example endpoint")
 * )
 */
 public function getUsers() {
    ...
 }

As per migration documentation you need to import Annotations class use OpenApi\Annotations as OA; that will fix your problem.

Related