> ## Documentation Index
> Fetch the complete documentation index at: https://omer-914cc1c6.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 复杂数据类型

> 使用 `oneOf`、`anyOf` 和 `allOf` 关键字，以灵活的 schema、可选属性和多种数据格式来描述 API

当你的 API 接受多种数据格式、包含条件字段或采用继承模式时，OpenAPI 的 schema 组合关键字可以帮助你记录这些灵活的结构。通过使用 `oneOf`、`anyOf` 和 `allOf`，你可以描述既能处理不同输入类型、又能将多个 schema 组合成完整数据模型的 API。

<div id="oneof-anyof-allof-keywords">
  ## `oneOf`, `anyOf`, `allOf` 关键字
</div>

对于复杂数据类型，OpenAPI 提供了用于组合 schema 的关键字：

* `allOf`：将多个 schema 组合在一起（类似合并对象或扩展基础 schema）。作用相当于 `and` 运算符。
* `anyOf`：接受与提供的任一 schema 匹配的数据。作用相当于 `or` 运算符。
* `oneOf`：只接受与提供的 schema 中恰好一个匹配的数据。作用相当于 `exclusive-or` 运算符。

<Warning>Mintlify 会将 `oneOf` 和 `anyOf` 以相同方式处理，因为二者在实际使用 API 时的差异很少产生影响。</Warning>

有关这些关键字的详细规范，请参见 [OpenAPI 文档](https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/)。

<Info>`not` 关键字目前不受支持。</Info>

<div id="combining-schemas-with-allof">
  ### 使用 `allOf` 组合 schema
</div>

当你使用 `allOf` 时，Mintlify 会对你的 OpenAPI 文档进行一些预处理，以便以更易读的方式展示复杂的组合。例如，当你用 `allOf` 组合两个对象 schema 时，Mintlify 会将两者的属性合并为一个对象。当你利用 OpenAPI 的可复用 [components](https://swagger.io/docs/specification/components/) 时，这一点尤其有用。

```yaml theme={null}
org_with_users:
  allOf:
    - $ref: '#/components/schemas/Org'
    - type: object
      properties:
        users:
          type: array
          description: 包含组织中所有用户的数组
# ...
components:
  schemas:
    Org:
      type: object
      properties:
        id:
          type: string
          description: 组织的 ID
```

<ParamField body="org_with_users" type="object">
  <Expandable>
    <ParamField body="id" type="string">
      组织的 ID
    </ParamField>

    <ParamField body="users" type="object[]">
      包含该组织所有用户的数组
    </ParamField>
  </Expandable>
</ParamField>

<div id="providing-options-with-oneof-and-anyof">
  ### 使用 `oneOf` 和 `anyOf` 提供选项
</div>

当你使用 `oneOf` 或 `anyOf` 时，选项会显示在标签页容器中。请在每个子架构中指定一个 `title` 字段，为各个选项命名。例如，以下演示如何展示两种不同类型的送货地址：

```yaml theme={null}
delivery_address:
  oneOf:
    - title: StreetAddress
      type: object
      properties:
        address_line_1:
          type: string
          description: 收件人的街道地址
        # ...
    - title: POBox
      type: object
      properties:
        box_number:
          type: string
          description: 邮政信箱的号码
        # ...
```

<ParamField body="delivery_address" type="object">
  <div className="mt-4 rounded-xl border border-gray-100 px-4 pb-4 pt-2 dark:border-white/10">
    <Tabs>
      <Tab title="StreetAddress">
        <ParamField body="address_line_1" type="string">
          住址的街道
        </ParamField>
      </Tab>

      <Tab title="POBox">
        <ParamField body="box_number" type="string">
          邮政信箱号码
        </ParamField>
      </Tab>
    </Tabs>
  </div>
</ParamField>
