Default value does not match the type (OpenAPI 3.0)

A default value conflicts with its declared data type

Description

A schema’s default value must match the field’s type. For example, assigning the string "a" as the default for type: integer conflicts with the type definition.

Potential impact

Code generation or validation may fail, or a client using the default may encounter a type error.

Remediation

Correct default to match the actual type and permitted range. Also verify that the server’s behavior when a value is omitted agrees with the documented default.

Examples

This OpenAPI 3.0 example changes the default of an integer schema from the string "a" to the integer 1.

Before

json
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "integer",
                  "format": "int32",
                  "description": "the size of the pack the dog is from",
                  "default": "a",
                  "minimum": 0
                }
              }
            }
          }
        }
      }
    }
  }
}

After

json
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "integer",
                  "format": "int32",
                  "description": "the size of the pack the dog is from",
                  "default": 1,
                  "minimum": 0
                }
              }
            }
          }
        }
      }
    }
  }
}

References