Review required properties and default-value behavior

Distinguish required presence from default values and document which component applies a default.

Description

A field listed in required must be present even if it declares a default. Whether a client or server inserts the default depends on the implementation.

Potential impact

A client that omits a required field may get a validation error.

Remediation

Test a request with the field omitted. Document which component supplies a default and when. Keep the presence requirement and remove a default if it misleads callers.

Examples

The schema excerpts omit info and paths. Both require id.

Before

json
{
  "openapi": "3.0.0",
  "components": {
    "schemas": {
      "MyObject": {
        "type": "object",
        "required": [
          "id"
        ],
        "properties": {
          "id": {
            "type": "string",
            "default": "4056684e4e1347579362617ad82e5b4e"
          }
        }
      }
    }
  }
}

After

json
{
  "openapi": "3.0.0",
  "components": {
    "schemas": {
      "MyObject": {
        "type": "object",
        "required": [
          "id"
        ],
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string",
            "default": "guest"
          }
        }
      }
    }
  }
}

The revised schema gives optional name a default of guest. Check whether the application fills it in.

References