OpenAPI 3.1 New Features and JSON Schema Alignment
Explore OpenAPI 3.1 improvements over 3.0 including full JSON Schema 2020-12 support, webhooks section, nullable replaced with type arrays, const keyword, and if/then/else schemas.
Detailed Explanation
OpenAPI 3.1 New Features
OpenAPI 3.1 is a significant update that aligns the specification with JSON Schema 2020-12. This brings powerful schema features and simplifies the relationship between OpenAPI and JSON Schema.
Full JSON Schema Compatibility
The biggest change: OpenAPI 3.1 schemas are valid JSON Schema 2020-12:
# OpenAPI 3.0 (custom nullable)
name:
type: string
nullable: true
# OpenAPI 3.1 (JSON Schema standard)
name:
type: [string, "null"]
Type Arrays
Replace nullable with type arrays:
# 3.0
age:
type: integer
nullable: true
# 3.1
age:
type: [integer, "null"]
# Multiple types
value:
type: [string, integer]
The const Keyword
Fixed values without enum:
# 3.0
type:
type: string
enum: [user]
# 3.1
type:
const: user
Webhooks Section
New top-level section for webhook definitions:
openapi: "3.1.0"
webhooks:
newOrder:
post:
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/Order"
responses:
"200":
description: OK
Conditional Schemas (if/then/else)
Address:
type: object
properties:
country:
type: string
state:
type: string
province:
type: string
if:
properties:
country:
const: US
then:
required: [state]
else:
required: [province]
$ref with Siblings
In 3.0, $ref could not have sibling keywords. In 3.1:
# 3.0 — not valid
property:
$ref: "#/components/schemas/Base"
description: Override description
# 3.1 — valid
property:
$ref: "#/components/schemas/Base"
description: Override description
contentMediaType and contentEncoding
Document Base64-encoded properties:
avatar:
type: string
contentMediaType: image/png
contentEncoding: base64
$dynamicRef for Extensibility
For advanced schema extensibility:
$defs:
node:
type: object
properties:
value:
type: string
children:
type: array
items:
$dynamicRef: "#node"
Migration from 3.0
Key changes to make:
- Change
openapi: "3.0.x"toopenapi: "3.1.0" - Replace
nullable: truewithtype: [original_type, "null"] - Replace single-value
enumwithconst - Move webhook callbacks to the new
webhookssection - Add
$refsiblings where needed
Use Case
Teams upgrading from OpenAPI 3.0 to 3.1 benefit from full JSON Schema compatibility, enabling schema reuse across validation, documentation, and code generation without translation. The webhooks section is critical for event-driven architectures, and conditional schemas (if/then/else) enable complex business rule documentation that was impossible in 3.0.