nelmio_api_doc.yaml 中的模式部分是否需要?

Is the schemas section in the nelmio_api_doc.yaml required?

我一直在使用 Swagger-PHP 设置 Nelmio API Doc Bundle。一切都按预期工作,我似乎无法理解的唯一一件事 out/understand 是模式。

在用户控制器中我有以下注释:

     *     @OA\RequestBody(
     *         description="Updated user object",
     *         required=true,
     *       @OA\MediaType(
     *           mediaType="multipart/form-data",
     *           @OA\Schema(ref="#/components/schemas/User")
     *        )
     *     )

在我的 Entity/User class 中,我将架构定义如下:

/**
 * User
 *
 * @OA\Schema(schema="User")
 *
 * @ORM\Table(schema="app", name="users")
 * @ORM\Entity
 */
class User implements UserInterface

在用户控制器中我也定义了 use App\Entity\User;

在我看来,这足以找到架构,但它不起作用,否则我不会在这里发帖:)

我能够让它工作的唯一方法是 运行 vendor/bin/openapi --format yaml src 和 copy/paste 将架构输出到 nelmio_api_doc.yaml 文件中。这是架构部分 I copy/pasted:

        User:
          properties:
            first_name:
              type: string
            middle_name:
              type: string
            last_name:
              type: string
            initials:
              type: string
            username:
              type: string
            password:
              type: string
            status:
              type: integer
            email:
              type: string
            id:
              type: integer
            customer_id:
              type: integer
            locked:
              type: boolean
          type: object

所以我的问题是,这是解决问题的方法还是应该自动创建架构部分?

感谢您的任何见解。

NelmioApiDocBundle 不会加载所有文件以获取与 swagger-php 相对的注释,要加载模式,您应该使用 @Model 注释,请参阅 https://symfony.com/doc/current/bundles/NelmioApiDocBundle/index.html#use-models

在你的情况下,这将给出以下结果:

use Nelmio\ApiDocBundle\Annotation\Model;

     /**
     *     @OA\RequestBody(
     *         description="Updated user object",
     *         required=true,
     *       @OA\MediaType(
     *           mediaType="multipart/form-data",
     *           @OA\Schema(ref=@Model(type=User::class))
     *        )
     *     )
     */