Skip to content

Menu

OpenAPI Cheat Sheet: Paths, Components, $ref and Security

Checked against OpenAPI Specification OpenAPI 3.1.x and 3.2.1 (latest listed on spec.openapis.org); examples validated with @seriousme/openapi-schema-validator 2.12.0 and Redocly CLI 2.84.0 on 28 November 2026. Each section links to the official docs it was checked against.

This sheet is for developers who write or review OpenAPI documents by hand and want the field names without scrolling a 300-page spec. The examples use openapi: 3.1.1 because most tooling supports it, and the notes flag what the newer 3.2 line adds. The spec index at spec.openapis.org lists 3.2.1 as the newest 3.x release when this was checked.

When to use what

Pick 3.1 if your generators, gateways and docs renderers all claim support for it, and check before you jump to 3.2. A version bump in the openapi field is cheap; finding out that your client generator silently drops querystring parameters is not. Stay on 3.0 only if a tool you can't replace refuses 3.1, because 3.0 uses its own JSON Schema dialect with nullable: true instead of real type arrays.

Put anything you reference twice in components and point at it with $ref. Shared parameters (components/parameters), error bodies (components/responses) and schemas belong there. Keep one-off request bodies inline; a components section full of single-use entries is harder to read than the inline version.

Validate twice, for two different questions. A schema validator answers "is this a legal OpenAPI document?" A linter like Redocly answers "is it a good one?" (missing operationId, no summary, example.com servers). Our OpenAPI validator does the first in the browser. Once it passes, the OpenAPI to TypeScript tool turns it into types, and OpenAPI to MCP drafts MCP tool definitions from the same file. The type-safe API clients post covers the codegen side.

openapi and info are required, plus at least one of paths, components or webhooks. The newest versions are listed at https://spec.openapis.org/oas/.

openapi: 3.1.1

Declare the spec version; tools pick their parser from this string, so it must be exact

info: {title: Notes API, version: 1.0.0}

Name the API and give your document's own version, which is not the OpenAPI version

servers: [{url: https://api.example.com/v1}]

Set the base URL that every path is appended to

security: [{bearerAuth: []}]

Apply a security requirement to every operation unless an operation overrides it

jsonSchemaDialect: <dialect URI>

Change the default JSON Schema dialect for schemas that don't declare $schema; if unset, the OAS dialect is used

From the docs, not run by us
$self: https://example.com/openapi.yaml

Give the document its own URI, used as the base for relative $refs (3.2 only)

From the docs, not run by us

Path parameters must be declared with required: true; the validator rejected our test file when we changed it to false.

/notes/{noteId}:

Template a path; every {name} needs a matching in: path parameter

get: {operationId: getNote, summary: ...}

Define one operation; operationId must be unique and becomes the function name in generated clients

parameters: [{name: noteId, in: path, required: true, schema: {type: string}}]

Declare a path parameter; required: true is mandatory for in: path

in: query | header | cookie | path

Choose where a parameter lives; 3.2 adds querystring for the whole query string as one value

From the docs, not run by us
requestBody: {required: true, content: {application/json: {schema: ...}}}

Describe the body keyed by media type; bodies on GET are not described this way

From the docs, not run by us
responses: {'200': ..., '404': ..., default: ...}

List responses by status code as quoted strings; default covers every code you didn't list

query: {...}

Describe a QUERY request on a Path Item (3.2 only)

From the docs, not run by us
$ref: '#/components/schemas/Note'

Point at a reusable schema with a JSON Pointer inside the same file

$ref: './schemas/note.yaml'

Pull a schema from another file; bundle before publishing if your tools can't follow external refs

From the docs, not run by us
components: {parameters: {NoteId: ...}}

Define a parameter once and reference it from every path that uses it

{$ref: '...', summary: ..., description: ...}

Override the referenced object's summary or description at the point of use (3.1+)

From the docs, not run by us
components: {responses: {NotFound: ...}}

Share an error response body across operations

From the docs, not run by us

In 3.1 the Schema Object is a superset of JSON Schema draft 2020-12, so these keywords work as JSON Schema documents them. Test schemas on their own with the JSON Schema validator.

type: [array, 'null']

Allow null the 3.1 way; nullable: true is a 3.0 keyword

required: [id, title]

List required properties at the object level, not on each property

const: active

Fix a value; shorter than a one-item enum

From the docs, not run by us
examples: [{id: n1, title: Hi}]

Give schema examples as an array, which is the JSON Schema form

From the docs, not run by us
oneOf: [{$ref: ...}, {$ref: ...}]

Accept exactly one of several shapes; pair it with discriminator for code generators

From the docs, not run by us
bearerAuth: {type: http, scheme: bearer, bearerFormat: JWT}

Describe bearer-token auth; bearerFormat is documentation only

apiKey: {type: apiKey, in: header, name: X-API-Key}

Describe an API key sent in a header, query or cookie

From the docs, not run by us
type: oauth2, flows: {authorizationCode: {...}}

Describe OAuth 2 with the flows your server supports and their scopes

From the docs, not run by us
security: []

Turn off auth for one operation, such as a health check

From the docs, not run by us
webhooks: {noteCreated: {post: {...}}}

Describe a request your API sends to subscribers, keyed by a name instead of a path

Worked example: Write a small 3.1 document and prove the validator catches a real mistake

Start with one path that reuses a parameter and a schema from components, plus a webhook and bearer auth (trimmed here):

openapi: 3.1.1
info: {title: Notes API, version: 1.0.0}
paths:
  /notes/{noteId}:
    parameters:
      - $ref: '#/components/parameters/NoteId'
    get:
      operationId: getNote
      summary: Get one note
      responses:
        '200':
          description: The note
components:
  parameters:
    NoteId: {name: noteId, in: path, required: true, schema: {type: string}}

Run it through a schema validator, then break it on purpose by setting required: false on the path parameter:

npx validate-api openapi.yaml
npx validate-api bad.yaml

The first run printed "valid": true. The second printed "valid": false with an error at /components/parameters/NoteId/required saying it must be equal to constant, with allowedValue: true. That is the spec rule that path parameters are always required, enforced by the official JSON Schema for 3.1. Redocly's recommended lint rules did not report this; on the first draft they complained instead about a missing summary and operationId on the webhook and about the example.com server URL. So run a schema validator for legality and a linter for style; neither replaces the other. Changing only the first line to openapi: 3.2.0 also validated, because nothing in this file uses a 3.2 feature.

Common mistakes

  • Writing nullable: true in a 3.1 document because an old tutorial did.

    Use a type array such as type: [string, 'null']. nullable belongs to 3.0's dialect and is not part of JSON Schema 2020-12.

  • Leaving response codes unquoted, as in 200: in YAML.

    Quote them ('200':). The spec says the keys are strings, and some parsers turn an unquoted 200 into a number.

  • Bumping openapi to 3.2.0 before checking the generators and gateways in the pipeline.

    Check each tool's release notes for 3.2 support. Nothing forces an upgrade if you don't need QUERY, querystring or streaming itemSchema.

  • Reusing the same operationId on two operations after copying a block.

    Make each one unique. Generated clients use it as the method name, so duplicates either fail or overwrite each other.

  • Treating a clean lint run as proof that the document is valid.

    Lint rules are configurable style checks. Run a validator built on the official OpenAPI JSON Schema as well.