首页 > 解决方案 > 如何在 Swagger 响应中指定特定参数而不是使用“allOf”?

问题描述

我已经在我的模型 (PHP) 中使用注释定义了一些参数,并darkaonline/l5-swagger用于将批次编译为 JSON。注释是这样的:

/** 
* @OA\Schema(schema="UserModel", required={""})
*/
class User extends Authenticatable
{

/**
 * @OA\Property(
 *   property="id",
 *   type="integer",
 *   format="int64",
 *   description="The unique identifier of a user"
 * )
 */

/**
 * @OA\Property(
 *   property="email",
 *   type="string",
 *   example="john@example.com",
 *   description="The email address of a user"
 * )
 */

/**
 * @OA\Property(
 *   property="password",
 *   type="string",
 *   description="The password of a user"
 * )
 */

/**
 * @OA\Schema(
 *   schema="CreateUser",
 *   type="object",
 *   allOf={
 *     @OA\Schema(ref="#/components/schemas/UserModel"),
 *   }
 * )
 */

在我为有效负载定义的架构中,我拥有的allOf将包括所有属性。就加载文档而言,这确实有效,但我显然不想要id那里的属性。我认为有一个替代方案allOf可以让我定义我想要的属性,但我似乎找不到它。

有谁知道它可能是什么?

标签: restapiswaggeropenapi

解决方案


openapi 中没有其他allOf选择可以让您从要使用的不同对象中选择哪些属性。如果您确实需要该功能,您可以分别定义包含每个字段的不同对象,然后重用您真正需要的对象。像这样的东西:

UserIdModel:
  properties:
    id: 
      type: integer
      ...
UserEmailModel:
  properties:
    email:
      type: string
      ...
UserPasswordModel:
  properties:
    password:
      type: string
      ...
CreateUser:
  allOf:
    - $ref: '#/components/schemas/UserEmailModel'
    - $ref: '#/components/schemas/UserPasswordModel'


推荐阅读