Incorrect request body reference (OpenAPI 3.0)

A request body references a schema or another incorrect object

Description

A $ref in requestBody must point to a Request Body Object that specifies the media type to send.

Potential impact

Pointing directly to a schema can leave the format undocumented and lead clients to send the wrong body.

Remediation

Use a request body under #/components/requestBodies/... or in an external file. Put the schema under the appropriate media type in that object’s content.

Examples

POST / points to #/components/requestBodies/List. The List definition specifies text/plain with a schema for an array of integers.

Before

json
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "components": {
    "requestBodies": {
      "List": {
        "description": "id of api version",
        "content": {
          "text/plain": {
            "schema": {
              "type": "array",
              "items": {
                "type": "integer"
              }
            }
          }
        }
      }
    }
  },
  "paths": {
    "/": {
      "post": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "requestBody": {
          "$ref": "#/components/schemas/List"
        },
        "responses": {
          "200": {
            "description": "200 response",
            "content": {
              "application/json": {
                "examples": {
                  "foo": {
                    "value": {
                      "versions": [
                        {
                          "status": "CURRENT",
                          "updated": "2011-01-21T11:33:21Z",
                          "id": "v2.0",
                          "links": [
                            {
                              "href": "http://127.0.0.1:8774/v2/",
                              "rel": "self"
                            }
                          ]
                        }
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

After

json
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "components": {
    "requestBodies": {
      "List": {
        "description": "id of api version",
        "content": {
          "text/plain": {
            "schema": {
              "type": "array",
              "items": {
                "type": "integer"
              }
            }
          }
        }
      }
    }
  },
  "paths": {
    "/": {
      "post": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "requestBody": {
          "$ref": "#/components/requestBodies/List"
        },
        "responses": {
          "200": {
            "description": "200 response",
            "content": {
              "application/json": {
                "examples": {
                  "foo": {
                    "value": {
                      "versions": [
                        {
                          "status": "CURRENT",
                          "updated": "2011-01-21T11:33:21Z",
                          "id": "v2.0",
                          "links": [
                            {
                              "href": "http://127.0.0.1:8774/v2/",
                              "rel": "self"
                            }
                          ]
                        }
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

References