L5-swagger api 文档:获取错误必需的 @OA\PathItem() 未找到

发布于 2025-01-11 11:04:22 字数 1745 浏览 0 评论 0原文

阅读 Stackoverflow 上的这两篇文章后: ​​如何解决 ErrorException:未找到必需的 @OA\PathItem() 无法在 l5-swagger 中生成 API 文档

运行 php artisan l5-swagger:generate 后,我仍然收到错误Required @OA\PathItem() not found

这是我的 Controller.php 部分:

/**
 * @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
{

这是我的 ProfileController 部分:

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

我在这里忽略了什么? 如果有人可以解释和提供帮助,那就太好了:)

编辑 - 解决方案

出现问题是因为我使用的是 laravel 模块包,并且我必须更改 l5-swagger.php 配置中的一些代码文件:

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

然后,我将主 Controller.php 从 App/Http/Controllers 复制到同一模块,以消除之后出现的 @OA\Info() not found 错误。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

弃爱 2025-01-18 11:04:22

当我第一次安装和配置时,我遇到了同样的错误。事实证明,仅 @OA\Info 不足以生成文档。除此之外,它还需要至少一个路径条目。添加 api 端点注释后,它得到修复。

例子:

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

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() {
    ...
 }
独自←快乐 2025-01-18 11:04:22

我在迁移到 v4.8.x 时遇到了这个问题,此时 doctrine/annotations 作为依赖项被删除。

您需要添加 doctrine/annotations 作为依赖项,或者更改为使用 PHP 注释,即,

use OpenApi\Attributes as OA;

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

如果

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

您不想一次全部迁移,则可以使用两种类型的注释。

命令:

composer require doctrine/annotations

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
小姐丶请自重 2025-01-18 11:04:22

根据迁移文档您需要导入 Annotations 类 use OpenApi\Annotations as OA; 这将解决您的问题。

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

写下不归期 2025-01-18 11:04:22
/**
 * Remove the specified resource from storage.
 *
 * @return \Illuminate\Http\JsonResponse
 */

如果我的index()函数上面有这些代码,应该立即删除它。这就是我收到此错误的原因

/**
 * 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

碍人泪离人颜 2025-01-18 11:04:22

当在 Laravel 中使用 OpenAPI 注解(例如 @OA\PathItem())而未安装必要的包时,通常会发生此错误。确保您已正确安装 darkaonline/l5-swagger 包,因为它在 Laravel 中提供对 OpenAPI 注释的支持。

如果您已经安装了该软件包,请确保您已正确遵循所有设置步骤,并且正确定义了路由和注释。如果问题仍然存在,您可能需要检查代码和配置以识别任何缺失或配置错误的元素。
输入图片此处描述

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文