Can't generate API documentation in l5-swagger

2021-01-23 10:15

阅读:455

You have to put some annotations in your code before running php artisan l5-swagger:generate. First, go to app/Http/Controllers/Controller.php and add a phpdoc comment block as follow before the class declaration:

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

This annotation by itself is sufficient to resolve the issue that you described, but if you execute php artisan l5-swagger:generate again you will see the following Exception:

 ErrorException  : Required @OA\PathItem() not found

  at /home/nathanael/dev/laravel-projects/laravel-swagger/vendor/zircote/swagger-php/src/Logger.php:39
    35|         $this->log = function ($entry, $type) {
    36|             if ($entry instanceof Exception) {
    37|                 $entry = $entry->getMessage();
    38|             }
  > 39|             trigger_error($entry, $type);
    40|         };
    41|     }
    42| 
    43|     /**

  Exception trace:

  1   trigger_error("Required @OA\PathItem() not found")
      /home/nathanael/dev/laravel-projects/laravel-swagger/vendor/zircote/swagger-php/src/Logger.php:39

  2   OpenApi\Logger::OpenApi\{closure}("Required @OA\PathItem() not found")
      /home/nathanael/dev/laravel-projects/laravel-swagger/vendor/zircote/swagger-php/src/Logger.php:71

  Please use the argument -v to see more details.

That‘s because you must have at least one method in a controller with an annotation that describes a route. You can easily create a resource in your application to test running php artisan make:controller ProjectsController -r and adding Route::resource(‘projects‘, ‘ProjectsController‘) to routes/web.php. After creating the controller open it and add the following phpdoc comment block before the index method, for example:

/**
 * @OA\Get(
 *     path="/projects",
 *     @OA\Response(response="200", description="Display a listing of projects.")
 * )
 */

Then, run php artisan l5-swagger:generate again and you must see a success message in the terminal.


评论


亲,登录后才可以留言!