Incorrect example object reference (OpenAPI 3.0)

An example references a schema or another wrong object type

Description

A $ref in examples must point to an Example Object. Supply the sample in value or link to it by URL in externalValue.

Potential impact

Pointing directly to a schema can leave readers without a usable example of the request data.

Remediation

Reference an example under #/components/examples/... or in an external file. Check its value against the relevant schema.

Examples

The Address schema requires a string named street. The request’s examples entry points to #/components/examples/Address, which supplies a value for that field.

Before

json
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API overview",
    "version": "1.0.0"
  },
  "components": {
    "securitySchemes": {
      "regularSecurity": {
        "type": "http",
        "scheme": "basic"
      }
    },
    "schemas": {
      "ErrorModel": {
        "type": "object",
        "properties": {
          "code": {
            "type": "string"
          }
        }
      },
      "Address": {
        "type": "object",
        "properties": {
          "street": {
            "type": "string"
          }
        },
        "required": [
          "street"
        ]
      }
    }
  },
  "paths": {
    "/": {
      "post": {
        "operationId": "updateAddress",
        "summary": "updateAddress",
        "servers": [
          {
            "url": "http://kicsapi.com/",
            "description": "server URL"
          }
        ],
        "responses": {
          "200": {
            "description": "The updated address",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Address"
                }
              }
            }
          },
          "default": {
            "description": "Unexpected error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorModel"
                }
              }
            }
          }
        },
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Address"
              },
              "examples": {
                "Address": {
                  "$ref": "#/components/schemas/Address"
                }
              }
            }
          }
        }
      }
    }
  }
}

After

json
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API overview",
    "version": "1.0.0"
  },
  "components": {
    "securitySchemes": {
      "regularSecurity": {
        "type": "http",
        "scheme": "basic"
      }
    },
    "schemas": {
      "ErrorModel": {
        "type": "object",
        "properties": {
          "code": {
            "type": "string"
          }
        }
      },
      "Address": {
        "type": "object",
        "properties": {
          "street": {
            "type": "string"
          }
        },
        "required": [
          "street"
        ]
      }
    },
    "examples": {
      "Address": {
        "summary": "user address",
        "value": {
          "street": "my street"
        }
      }
    }
  },
  "paths": {
    "/": {
      "post": {
        "operationId": "updateAddress",
        "summary": "updateAddress",
        "servers": [
          {
            "url": "http://kicsapi.com/",
            "description": "server URL"
          }
        ],
        "responses": {
          "200": {
            "description": "The updated address",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Address"
                }
              }
            }
          },
          "default": {
            "description": "Unexpected error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorModel"
                }
              }
            }
          }
        },
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Address"
              },
              "examples": {
                "Address": {
                  "$ref": "#/components/examples/Address"
                }
              }
            }
          }
        }
      }
    }
  }
}

References