Use OpenAPI examples as a contract: separate data from wire format
OpenAPI examples make request and response contracts concrete, but schema-ready data and its serialized HTTP representation can differ. In OpenAPI 3.2.1, use dataValue for the data a schema validates, and serializedValue or externalValue for what is sent on the wire. This distinction prevents examples from teaching clients to send the wrong bytes or encoding.
OpenAPI examples make request and response contracts concrete, but schema-ready data and its serialized HTTP representation can differ. In OpenAPI 3.2.1, use dataValue for the data a schema validates, and serializedValue or externalValue for what is sent on the wire. This distinction prevents examples from teaching clients to send the wrong bytes or encoding.
An example does not replace validation
In a Media Type Object, singular example and plural examples are mutually exclusive. Use the plural map and Example Objects when you need named examples, descriptions, or references. A Schema Object also has JSON Schema's examples array for data examples kept with the schema. OAS 3.2.1 marks the Schema Object's singular example as deprecated; avoid it for new documents unless compatibility requires it.
Examples SHOULD match the schema and the media type/encoding. This does not mean every viewer, linter, or generator automatically validates them. Check the exact tool and version, and add an explicit validation step for important examples.
JSON request body
Use an Example Object's dataValue to show the structure that the schema validates:
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [name, quantity]
properties:
name:
type: string
quantity:
type: integer
minimum: 1
examples:
twoItems:
summary: Order with quantity two
dataValue:
name: notebook
quantity: 2For JSON, the data form and transmitted form usually have the same structure, so a separate serialized example is often unnecessary. Add serializedValue only when showing the exact wire representation adds value; duplicated values can drift apart.
Form parameters: show serialization
Form encoding treats spaces and literal plus signs differently. The data value a + b becomes foo=a+%2B+b: the space becomes +, while the plus sign is percent-encoded.
parameters:
- name: filters
in: querystring
content:
application/x-www-form-urlencoded:
schema:
type: object
properties:
foo:
type: string
active:
type: boolean
examples:
filterExample:
summary: Filter containing a space and plus sign
dataValue:
foo: a + b
active: true
serializedValue: foo=a+%2B+b&active=trueHere dataValue is the schema-ready structure; serializedValue is the query string sent by the client. Confusing them can produce a different request or hide an encoding bug. Verify that the OAS 3.2.1 querystring and form media type fields are supported by your tooling.
Choose a field by purpose
| Purpose | Field | Notes |
|---|---|---|
| Keep schema-valid data with its schema | Schema Object examples array | JSON Schema Draft 2020-12 keyword |
| Named request/response or parameter example | Example Object dataValue | Place it in the parent object's examples map |
| Show a textual HTTP representation | serializedValue | Must accurately represent serialization and encoding |
| Binary or separately maintained serialized example | externalValue | Maintain file access, path, and version alongside the contract |
| Simple JSON value for OAS 3.1-or-earlier compatibility | value or shorthand example | Safe when JSON/YAML data and serialization are effectively identical; non-JSON behavior may vary by tool |
In OAS 3.2.1, prefer the Schema Object's plural examples over its deprecated singular example. On Parameter, Header, and Media Type Objects, singular example and plural examples cannot coexist. If legacy compatibility requires an older field, document the target OAS version and tool behavior.
Review examples as contract material
Does each example match its media type, encoding, and parameter serialization?
Are schema-ready JSON values expressed with `dataValue` or Schema Object `examples`?
Where delimiters, percent-encoding, or character encoding matter, is the serialized form shown?
Do required and optional fields and boundary values match the schema?
Are relevant error responses included as well as success cases?
Have the actual viewer, linter, or generator versions been checked for field support?
An example illustrates a case; it does not define the full set of accepted inputs or prove server behavior. Schema validation, example compatibility checks, and consumer-provider contract verification are separate checks. Include the checks your release process needs.
Sources
[OpenAPI Specification 3.2.1: Example Object and Working with Examples](https://spec.openapis.org/oas/v3.2.1.html#example-object)
[OpenAPI Specification 3.2.1: Media Type Object](https://spec.openapis.org/oas/v3.2.1.html#media-type-object)
[OpenAPI Specification 3.2.1: Parameter Object](https://spec.openapis.org/oas/v3.2.1.html#parameter-object)