Review schema definitions for required properties

Separate required presence from value constraints and define both to match the API contract.

Description

required names fields that must be present. properties constrains their values. A required field may be defined in another schema, such as an allOf branch.

Potential impact

A field with no value constraints can accept unintended types. A closed schema that forbids a required field can reject every instance of the object.

Remediation

Check all branches and additionalProperties. Keep the field in required if callers must supply it, and define its allowed values where the contract needs them.

Examples

These schema excerpts omit info and paths. Both require name.

Before

json
{
  "openapi": "3.0.0",
  "components": {
    "schemas": {
      "Example": {
        "type": "object",
        "required": [
          "name"
        ],
        "properties": {
          "age": {
            "type": "integer"
          }
        }
      }
    }
  }
}

After

json
{
  "openapi": "3.0.0",
  "components": {
    "schemas": {
      "Example": {
        "type": "object",
        "required": [
          "name"
        ],
        "properties": {
          "name": {
            "type": "string"
          },
          "age": {
            "type": "integer"
          }
        }
      }
    }
  }
}

Adding type: string rejects values for name that previously passed, such as a number.

References