如何在 OpenAPI 3.0 中要求对象至少具有一个属性?

gat*_*man 5 swagger openapi

我有这个 OpenAPI 架构:

components:
  schemas:
    User:
      type: object
      required:
        - username
        - email
        - description
        - profile_icon
      properties:
        username
          type: string
        email
          type: string
          format: email
        description
          type: string
        profile_icon
          type: string
Run Code Online (Sandbox Code Playgroud)

所有属性都是必需的。这是为了PUT /user/profile.

我会将其更改为PATCH /user/profile. 用户仅发送他们请求更改的参数。系统验证至少需要一个参数。

我如何描述 [用户名、电子邮件、描述、个人资料图标] 之一是必需的?

可接受的示例请求:

{
  "username": "Will Smith"
}
Run Code Online (Sandbox Code Playgroud)
{
  "email": "test@example.com",
  "profile_icon": "aaaaa.png"
}
Run Code Online (Sandbox Code Playgroud)

不可接受的示例(错误):

{}
Run Code Online (Sandbox Code Playgroud)

注释anyOf器很接近,但它似乎仅适用于$ref模式,而不适用于属性。

https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/

Hel*_*len 6

要求对象中至少有 1 个属性的最简单方法是使用minProperties: 1.

    User:
      type: object
      minProperties: 1   # <--------
      properties:
        username:
          type: string
        ...
Run Code Online (Sandbox Code Playgroud)