> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify-mintlify-add-hello-world-quickstart-48843.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 复杂数据类型

> 使用 `oneOf`、`anyOf` 和 `allOf` 关键字，为具有灵活模式、可选属性和多种数据格式的 API 建模与描述

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

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

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

* `allOf`：组合多个模式（类似合并对象或扩展基础模式），相当于 `and` 运算符。
* `anyOf`：接受与提供的任一模式匹配的数据，相当于 `or` 运算符。
* `oneOf`：仅接受与提供的模式中恰好一个匹配的数据，相当于“异或”运算符。

<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` 组合模式
</div>

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

```yaml
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
delivery_address:
  oneOf:
    - title: 街道地址
      type: object
      properties:
        address_line_1:
          type: string
          description: 收件人的街道地址
        # ...
    - title: 邮政信箱
      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>
