Skip to content
ADP
API Design PrincipleBETA

[ADP-758] OpenAPI: $ref

Overview

In OpenAPI specifications, there are two primary methods for referencing reusable components:

  1. Internal References: Using #/components/* within the same OpenAPI document.
  2. External References: Using separate YAML files hosted on a public site or within the project repository.

Guidance

Example: Defining and Using Internal References

  • Defining Components:
yaml
components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: string
        name:
          type: string
        email:
          type: string
  • Referencing Components:
yaml
paths:
  /users:
    get:
      summary: Retrieve all users
      responses:
        '200':
          description: A list of users
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/User'

Example: Defining and Using External References

  • User Schema in an External File (https://example.com/openapi/schema.yaml):
yaml
User:
  type: object
  properties:
    id:
      type: string
    name:
      type: string
    email:
      type: string
  • Referencing the External Schema in the Main OpenAPI Document:
yaml
openapi: 3.0.0
info:
  title: User API
  version: 1.0.0
paths:
  /users:
    get:
      summary: Retrieve all users
      responses:
        '200':
          description: A list of users
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: 'https://example.com/openapi/schema.yaml#/User'

Component Types

The components section can include various reusable elements:

Schemas

Define data models used in request and response bodies.

yaml
components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: string
        name:
          type: string
        email:
          type: string

Responses

Define reusable response templates for common HTTP responses.

yaml
components:
  responses:
    NotFound:
      description: Resource not found
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'

Parameters

Define reusable query parameters, path parameters, headers, etc.

yaml
components:
  parameters:
    PageSize:
      name: pageSize
      in: query
      description: Number of results to return per page
      required: false
      schema:
        type: integer
        default: 10

Examples

Provide examples for request bodies, responses, and parameters to illustrate usage.

yaml
components:
  examples:
    UserExample:
      summary: A user example
      value:
        id: '1'
        name: 'John Doe'
        email: 'john.doe@example.com'

Request Bodies

Define reusable request body structures.

yaml
components:
  requestBodies:
    UserRequest:
      description: User to create
      required: true
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/User'

Headers

Define reusable headers.

yaml
components:
  headers:
    RateLimit:
      description: The number of allowed requests in the current period
      schema:
        type: integer

Security Schemes

Define reusable security schemes.

yaml
components:
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: X-API-Key

References

INFO

💡 Certain OpenAPI edit tool doesn't support editing the #/components/parameters field of the OpenAPI document; furthermore, we assume OpenAPI documents could be owned/managed by different groups in the business unit/company.