7

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="[email protected]"
 *      ),
 * ),
 *  @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.

3
  • I think this boils down to swagger-php requiring at least one endpoint. In the case above, it was not looking for annotations in the right path, in mine... I had none, yet. Commented May 11, 2023 at 13:59
  • Thanks, Xavvey, your solution lead me to fix the issue! :) Commented Feb 28, 2024 at 14:27
  • After installing darkaonline/l5-swagger, Ensure your application has the necessary Swagger annotations throughout the controllers and models and the run this command again composer update and then try again php artisan l5-swagger:generate Commented Jun 3, 2024 at 7:59

6 Answers 6

10

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() {
    ...
 }
Sign up to request clarification or add additional context in comments.

Comments

2

I got this when migrating to v4.8.x, which is when doctrine/annotations was removed as a dependency.

You either need to add doctrine/annotations as a dependency or change to using PHP annotations i.e.

use OpenApi\Attributes as OA;

#[OA\Info(
    version: '1.0.0',
    title: 'My API'
)]

instead of

/**
 * @OA\Info(
 *   version="1.0.0",
 *   title="My API"
 * )
 */

You can use both types of annotations if you have a lot that you don't want to migrate all at once.

Command:

composer require doctrine/annotations

Comments

0

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

Comments

0
/**
 * Remove the specified resource from storage.
 *
 * @return \Illuminate\Http\JsonResponse
 */

if there are these codes above my index() function, it should be deleted immediately. that's why I got this error

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0
/**
* @OA\PathItem(path="/api/v1")
*
* @OA\Info(
*      version="0.0.0",
*      title="kashtnegar API"
*  )
*/

Put this in the route of your controller. For me it is Http/controllers/controller.php.

1 Comment

Thank you for your interest in contributing to the Stack Overflow community. This question already has a few answers—including at least one that has been validated by the community. It would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?
-1

This error typically occurs when using OpenAPI annotations (such as @OA\PathItem()) in Laravel without having the necessary packages installed. Make sure you have installed the darkaonline/l5-swagger package properly, as it provides support for OpenAPI annotations in Laravel.

If you have already installed the package, ensure that you have followed all the setup steps correctly and that your routes and annotations are properly defined. If the issue persists, you may need to review your code and configuration to identify any missing or misconfigured elements. enter image description here

1 Comment

Please share more details, such that others can learn from your answer, and share all details in text form

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.