Skip to main content

OpenAPI Schema Guide: Nullable Properties, Dates, Required Fields, $ref, Server URLs & Bearer Auth

· 8 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

Defining consistent API contracts in OpenAPI (Swagger) requires handling several version-specific schema rules cleanly across OpenAPI 2.0, 3.0, and 3.1—from nullable properties and required fields to date formats, reusable $ref parameters, dynamic server URLs, and Bearer authentication.

1. Defining a Nullable Property Across Versions​

To define a property that can be a string or null, the syntax depends on the OpenAPI specification version.

OpenAPI 3.1.0​

OpenAPI 3.1.0 and later versions align with the JSON Schema specification, which simplifies the definition of nullable types. You can use an array for the type keyword to specify multiple possible types, including null.

Example:

openapi: 3.1.0
info:
title: API
version: 1.0.0
paths: {}
components:
schemas:
User:
type: object
properties:
username:
type: string
description: A required string
email:
type: [string, "null"]
description: Can be a string or null

In this example, the email property can be either a string or null. This is the most straightforward and modern way to define a nullable type in OpenAPI.


OpenAPI 3.0.0​

In OpenAPI 3.0.0, the nullable: true keyword was introduced specifically to define properties that can be null.

Example:

openapi: 3.0.0
info:
title: API
version: 1.0.0
paths: {}
components:
schemas:
User:
type: object
properties:
username:
type: string
description: A required string
email:
type: string
nullable: true
description: Can be a string or null

In this version, you must specify the primary type (e.g., string, integer) and then add nullable: true to indicate that null is also a valid value. Do not use an array for type as it is not supported in this version.


OpenAPI 2.0 (Swagger 2.0)​

Swagger 2.0 does not have a dedicated nullable keyword or support for multiple types. The convention for defining a property that could be null was to document it in the description.

Example:

swagger: '2.0'
info:
title: API
version: 1.0.0
paths: {}
definitions:
User:
type: object
properties:
username:
type: string
description: A required string
email:
type: string
description: Can be a string or null

For Swagger 2.0, the information that the email property is nullable is conveyed solely through the description field. This means that code generators and validation tools would not automatically enforce the nullability. It relies on the consumer of the API documentation to understand this convention.

The lack of a formal way to define nullability was one of the key reasons the nullable keyword was added in OpenAPI 3.0.0 to improve clarity and machine-readability of the specification.


2. required Fields Across OpenAPI Versions​

required is always declared as a list of property names at the object schema level (never inline on a single property). In Swagger 2.0 it sits under definitions.<Model>.required; in OpenAPI 3.0 and 3.1 it moves under components.schemas.<Model>.required:

components:
schemas:
User:
type: object
required:
- id
- name
properties:
id:
type: integer
name:
type: string
email:
type: string

3. Declaring Dates and Date-Times (ISO 8601)​

Use type: string combined with standard format keywords:

  • format: date for calendar dates (YYYY-MM-DD)
  • format: date-time for full ISO 8601 timestamps (2026-01-15T10:30:00Z)
properties:
eventDate:
type: string
format: date
eventDateTime:
type: string
format: date-time

4. Reusable Parameters with $ref​

Centralize shared path and query parameters (such as userId, page, and pageSize) inside components.parameters and reference them via #/components/parameters/<Name>:

paths:
/posts:
get:
parameters:
- $ref: '#/components/parameters/PageParameter'

components:
parameters:
PageParameter:
name: page
in: query
required: false
schema:
type: integer
default: 1
minimum: 1

5. Bearer Token Authentication (Authorization: Bearer)​

In OpenAPI 3.0 and 3.1, define an http scheme with scheme: bearer under components.securitySchemes and apply it globally or per-operation:

components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT

security:
- bearerAuth: []

In legacy Swagger 2.0 (which lacks scheme: bearer), define type: apiKey, in: header, name: Authorization under securityDefinitions.


6. Handling Environment Variables in servers URLs​

Automated Swagger 2.0-to-3.0 converters (such as swagger2openapi) turn host placeholders into default: unknown, which breaks Swagger UI base URLs and SDK generators:

servers:
- url: https://{env}-gateway-{projectId}.run.app
variables:
env:
default: dev
projectId:
default: xyz123
StrategyWhen to Use
Safe placeholder default valuesShared public docs where real host IDs stay private
Post-process servers in build scriptGenerating separate baked specs for dev, staging, and prod
CI templating (envsubst / Python)Injecting real deployment URLs at build time without committing secrets

7. Design-First (API-First) Workflow in Python​

Under a Design-First workflow, the OpenAPI spec acts as the contract before backend code is written:

  1. Author the OpenAPI YAML contract (Stoplight Studio, Swagger Editor) so frontend and QA teams can build against mock servers immediately (openapi-core, connexion).
  2. Generate or bind Python models (datamodel-code-generator for Pydantic, or FastAPI's built-in OpenAPI schema generation) and validate requests/responses in CI.

Sources & Technical References​