These patterns are commonly challenging. Brief examples below with links to detailed guidance.
Enums
Use string enums with clear, semantic values:
```yaml
type: string
enum:
- pending
- approved
- rejected
- cancelled
```
Avoid:
- Numeric strings ("0", "1", "2")
- Generic values ("value1", "value2")
- Unclear abbreviations ("pnd", "appr")
See [reference/schemas.md#enums](reference/schemas.md) for more.
Polymorphism (oneOf/allOf/anyOf)
oneOf: Value matches exactly one schema (type discrimination)
```yaml
PaymentMethod:
oneOf:
- $ref: '#/components/schemas/CreditCard'
- $ref: '#/components/schemas/BankAccount'
- $ref: '#/components/schemas/PayPal'
discriminator:
propertyName: type
mapping:
credit_card: '#/components/schemas/CreditCard'
bank_account: '#/components/schemas/BankAccount'
paypal: '#/components/schemas/PayPal'
```
allOf: Value matches all schemas (composition/inheritance)
```yaml
AdminUser:
allOf:
- $ref: '#/components/schemas/User'
- type: object
properties:
permissions:
type: array
items: {type: string}
```
anyOf: Value matches one or more schemas (flexible union)
```yaml
SearchFilter:
anyOf:
- $ref: '#/components/schemas/TextFilter'
- $ref: '#/components/schemas/DateFilter'
- $ref: '#/components/schemas/NumericFilter'
```
See [reference/schemas.md#polymorphism](reference/schemas.md) for detailed guidance.
Discriminators
Use discriminators with oneOf for efficient type identification:
```yaml
Pet:
oneOf:
- $ref: '#/components/schemas/Dog'
- $ref: '#/components/schemas/Cat'
discriminator:
propertyName: petType
mapping:
dog: '#/components/schemas/Dog'
cat: '#/components/schemas/Cat'
Dog:
type: object
required: [petType, bark]
properties:
petType:
type: string
enum: [dog]
bark:
type: string
Cat:
type: object
required: [petType, meow]
properties:
petType:
type: string
enum: [cat]
meow:
type: string
```
See [reference/schemas.md#discriminators](reference/schemas.md) for more.
Nullable Types
Handle null values differently based on OpenAPI version:
OpenAPI 3.1 (JSON Schema 2020-12 compliant):
```yaml
type: [string, "null"]
# or
type: string
nullable: true # Still supported for compatibility
```
OpenAPI 3.0:
```yaml
type: string
nullable: true
```
For optional fields, use required array:
```yaml
type: object
properties:
name: {type: string} # Can be omitted
email: {type: string} # Can be omitted
required: [name] # email is optional
```
See [reference/schemas.md#nullable](reference/schemas.md) for more.
File Uploads
Use multipart/form-data for file uploads:
```yaml
requestBody:
required: true
content:
multipart/form-data:
schema:
type: object
properties:
file:
type: string
format: binary
metadata:
type: object
properties:
description: {type: string}
tags:
type: array
items: {type: string}
required: [file]
```
For base64-encoded files in JSON:
```yaml
requestBody:
content:
application/json:
schema:
type: object
properties:
filename: {type: string}
content:
type: string
format: byte # base64-encoded
```
See [reference/request-bodies.md#file-uploads](reference/request-bodies.md) for more.
Server-Sent Events (SSE)
Express streaming responses with text/event-stream:
```yaml
responses:
'200':
description: Stream of events
content:
text/event-stream:
schema:
type: string
description: |
Server-sent events stream. Each event follows the format:
```
event: message
data: {"type": "update", "content": "..."}
```
examples:
notification_stream:
value: |
event: message
data: {"type": "notification", "message": "New order received"}
event: message
data: {"type": "notification", "message": "Order processing complete"}
```
See [reference/responses.md#streaming](reference/responses.md) for more patterns.