|
| 1 | +--- |
| 2 | +title: "Complex data types" |
| 3 | +description: "Describe APIs with flexible schemas, optional properties, and multiple data formats using `oneOf`, `anyOf`, and `allOf` keywords" |
| 4 | +--- |
| 5 | + |
| 6 | +When your API accepts multiple data formats, has conditional fields, or uses inheritance patterns, OpenAPI's schema composition keywords help you document these flexible structures. Using `oneOf`, `anyOf`, and `allOf`, you can describe APIs that handle different input types or combine multiple schemas into comprehensive data models. |
| 7 | + |
| 8 | +## `oneOf`, `anyOf`, `allOf` keywords |
| 9 | + |
| 10 | +For complex data types, OpenAPI provides keywords for combining schemas: |
| 11 | + |
| 12 | +- `allOf`: Combines multiple schemas (like merging objects or extending a base schema). Functions like an `and` operator. |
| 13 | +- `anyOf`: Accepts data matching any of the provided schemas. Functions like an `or` operator. |
| 14 | +- `oneOf`: Accepts data matching exactly one of the provided schemas. Functions like an `exclusive-or` operator. |
| 15 | + |
| 16 | +<Warning>Mintlify treats `oneOf` and `anyOf` identically since the practical difference rarely affects using the API.</Warning> |
| 17 | + |
| 18 | +For detailed specifications of these keywords see the [OpenAPI documentation](https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/). |
| 19 | + |
| 20 | +<Info>The `not` keyword is currently unsupported.</Info> |
| 21 | + |
| 22 | +### Combining schemas with `allOf` |
| 23 | + |
| 24 | +When you use `allOf`, Mintlify performs some preprocessing on your OpenAPI document to display complex combinations in a readable way. For example, when you combine two object schemas with `allOf`, Mintlify combines the properties of both into a single object. This becomes especially useful when leveraging OpenAPI's reusable [components](https://swagger.io/docs/specification/components/). |
| 25 | + |
| 26 | +```yaml |
| 27 | +org_with_users: |
| 28 | + allOf: |
| 29 | + - $ref: '#/components/schemas/Org' |
| 30 | + - type: object |
| 31 | + properties: |
| 32 | + users: |
| 33 | + type: array |
| 34 | + description: An array containing all users in the organization |
| 35 | +... |
| 36 | +components: |
| 37 | + schemas: |
| 38 | + Org: |
| 39 | + type: object |
| 40 | + properties: |
| 41 | + id: |
| 42 | + type: string |
| 43 | + description: The ID of the organization |
| 44 | +``` |
| 45 | +
|
| 46 | +<ParamField body="org_with_users" type="object"> |
| 47 | + <Expandable> |
| 48 | + <ParamField body="id" type="string"> |
| 49 | + The ID of the organization |
| 50 | + </ParamField> |
| 51 | + <ParamField body="users" type="object[]"> |
| 52 | + An array containing all users in the organization |
| 53 | + </ParamField> |
| 54 | + </Expandable> |
| 55 | +</ParamField> |
| 56 | +
|
| 57 | +### Providing options with `oneOf` and `anyOf` |
| 58 | + |
| 59 | +When you use `oneOf` or `anyOf`, the options are displayed in a tabbed container. Specify a `title` field in each subschema to give your options names. For example, here's how you might display two different types of delivery addresses: |
| 60 | + |
| 61 | +```yaml |
| 62 | +delivery_address: |
| 63 | + oneOf: |
| 64 | + - title: StreetAddress |
| 65 | + type: object |
| 66 | + properties: |
| 67 | + address_line_1: |
| 68 | + type: string |
| 69 | + description: The street address of the recipient |
| 70 | + ... |
| 71 | + - title: POBox |
| 72 | + type: object |
| 73 | + properties: |
| 74 | + box_number: |
| 75 | + type: string |
| 76 | + description: The number of the PO Box |
| 77 | + ... |
| 78 | +``` |
| 79 | + |
| 80 | +<ParamField body="delivery_address" type="object"> |
| 81 | + <div className="mt-4 rounded-xl border border-gray-100 px-4 pb-4 pt-2 dark:border-white/10"> |
| 82 | + <Tabs> |
| 83 | + <Tab title="StreetAddress"> |
| 84 | + <ParamField body="address_line_1" type="string"> |
| 85 | + The street address of the residence |
| 86 | + </ParamField> |
| 87 | + </Tab> |
| 88 | + <Tab title="POBox"> |
| 89 | + <ParamField body="box_number" type="string"> |
| 90 | + The number of the PO Box |
| 91 | + </ParamField> |
| 92 | + </Tab> |
| 93 | + </Tabs> |
| 94 | + </div> |
| 95 | +</ParamField> |
0 commit comments