l5-swagger/swagger-php - 将组件添加到块使其从输出中消失

发布于 2025-01-10 05:46:45 字数 970 浏览 2 评论 0原文

如果我将此响应添加到我的定义中:

@OA\Response(
    response="default",
    description="unexpected error",
    @OA\JsonContent(ref="#/components/schemas/ErrorModel"),
    @OA\XmlContent(ref="#/components/schemas/ErrorModel"),
    @OA\MediaType(
        mediaType="text/xml",
        @OA\Schema(ref="#/components/schemas/ErrorModel")
        ),
        @OA\MediaType(
          mediaType="text/html",
          @OA\Schema(ref="#/components/schemas/ErrorModel")
        )
 )

然后我将架构放在下面,如下所示:

/**
 * @OA\Schema(
 *     schema="ErrorModel",
 *     required={"code", "message"},
 *     @OA\Property(
 *         property="code",
 *         type="integer",
 *         format="int32"
 *     ),
 *     @OA\Property(
 *         property="message",
 *         type="string"
 *     )
 * )
 */

命令: php artisan l5-swagger:generate 不会出错,但包含带有组件的响应定义的块不再包含在 json 中但组件的架构呢?

我是否做了一些非常明显错误的事情,因为到目前为止我在图书馆的经验是,如果你做错了什么,它通常会告诉你。

If I add this response to my definition:

@OA\Response(
    response="default",
    description="unexpected error",
    @OA\JsonContent(ref="#/components/schemas/ErrorModel"),
    @OA\XmlContent(ref="#/components/schemas/ErrorModel"),
    @OA\MediaType(
        mediaType="text/xml",
        @OA\Schema(ref="#/components/schemas/ErrorModel")
        ),
        @OA\MediaType(
          mediaType="text/html",
          @OA\Schema(ref="#/components/schemas/ErrorModel")
        )
 )

And then I place the Schema underneath like so:

/**
 * @OA\Schema(
 *     schema="ErrorModel",
 *     required={"code", "message"},
 *     @OA\Property(
 *         property="code",
 *         type="integer",
 *         format="int32"
 *     ),
 *     @OA\Property(
 *         property="message",
 *         type="string"
 *     )
 * )
 */

The command: php artisan l5-swagger:generate does not error but the block that contains the Response Definition with the component no longer gets included in the json but the schema for the component does?

Did I do something really obvious that is wrong as my experience with the library so far is if you do something wrong it generally tells you.

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

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

发布评论

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

评论(2

自控 2025-01-17 05:46:45

如果这是一个新项目,则更改是您现在使用的是 swagger-php V4。在版本 4 中,分析器代码使用反射。这样做是为了可以使用注释或 PHP 8 属性。

一个缺点是不再检测到独立的文档块,因为没有反射来访问这些文档块。

在你的情况下,我认为有两个单独的 /** */ 块意味着只找到最后一个。

一种解决方法是将类前面的所有注释放在单个 /** */ 块中。
另一种选择是针对此类情况使用(未使用的)虚拟类。

在这里,假设第一个注释有一个单独的响应类,那么在您的架构中添加 class ErrorModel {} 也应该可以工作。

这同样适用于其他顶级注释,例如 @OA\Info 或其他注释。

If this is a new projects changes are that you are now using swagger-php V4. In version 4 the analyser code uses reflection. This was done to make it possible to use either annotations or PHP 8 attributes.

One downside is that stand-alone docblocks are no longer detected as there is no reflection to access those.

In your case I think having two separate blocks of /** */ will mean only the last one is found.

One way around is to have all annotations preceding a class in a single /** */ block.
Another option is to have (unused) dummy classes for cases like this.

Here, adding class ErrorModel {} fater your schema should work too, assuming there is a separate resonse class for the first annotation.

The same applies to other top level annotations like @OA\Info or others.

分分钟 2025-01-17 05:46:45

注释不能分开,应位于同一块中

示例:

更改此:

/** 
 * @OA\Info( 
 *      version="1.0.0", 
 *      title="Service",
 *      description="Service",
 *      @OA\Contact(
 *          email="[email protected]"
 *      ),
 *     @OA\License(
 *         name="Name"
 *     ),
 * ),
 */

/**
 * @OA\Schema(
 *     schema="ErrorModel",
 *     required={"code", "message"},
 *     @OA\Property(
 *         property="code",
 *         type="integer",
 *         format="int32"
 *     ),
 *     @OA\Property(
 *         property="message",
 *         type="string"
 *     )
 * )
 */

为此:

/**
 * @OA\Info(
 *      version="1.0.0",
 *      title="Service",
 *      description="Service",
 *      @OA\Contact(
 *          email="[email protected]"
 *      ),
 *     @OA\License(
 *         name="Name"
 *     ),
 * ),
 *
 * @OA\Schema(
 *     schema="ErrorModel",
 *     required={"code", "message"},
 *     @OA\Property(
 *         property="code",
 *         type="integer",
 *         format="int32"
 *     ),
 *     @OA\Property(
 *         property="message",
 *         type="string"
 *     )
 * )
 */

The annotations cannot be separate, should be in the same block

example:

change this:

/** 
 * @OA\Info( 
 *      version="1.0.0", 
 *      title="Service",
 *      description="Service",
 *      @OA\Contact(
 *          email="[email protected]"
 *      ),
 *     @OA\License(
 *         name="Name"
 *     ),
 * ),
 */

/**
 * @OA\Schema(
 *     schema="ErrorModel",
 *     required={"code", "message"},
 *     @OA\Property(
 *         property="code",
 *         type="integer",
 *         format="int32"
 *     ),
 *     @OA\Property(
 *         property="message",
 *         type="string"
 *     )
 * )
 */

for this:

/**
 * @OA\Info(
 *      version="1.0.0",
 *      title="Service",
 *      description="Service",
 *      @OA\Contact(
 *          email="[email protected]"
 *      ),
 *     @OA\License(
 *         name="Name"
 *     ),
 * ),
 *
 * @OA\Schema(
 *     schema="ErrorModel",
 *     required={"code", "message"},
 *     @OA\Property(
 *         property="code",
 *         type="integer",
 *         format="int32"
 *     ),
 *     @OA\Property(
 *         property="message",
 *         type="string"
 *     )
 * )
 */

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