Review additional property policy in OpenAPI 2.0

Choose additionalProperties to match the data contract.

Description

In OpenAPI 2.0, additionalProperties controls fields outside properties. Both Boolean values and a value schema are valid.

Potential impact

Too broad a rule accepts unexpected fields. Too narrow a rule rejects fields that clients use.

Remediation

Set false for a closed object. Use a schema for dynamic keys whose values share a type. Use true or omit the setting when any extra value is allowed. Test the chosen rule in the server’s validator.

Examples

The response starts with a closed set of fields.

Before

json
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response",
            "schema": {
              "type": "object",
              "properties": {
                "name": {
                  "type": "string"
                },
                "tag": {
                  "type": "string"
                }
              },
              "required": [
                "name"
              ],
              "additionalProperties": false
            }
          }
        }
      }
    }
  }
}

After

json
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response",
            "schema": {
              "type": "object",
              "properties": {
                "name": {
                  "type": "string"
                },
                "tag": {
                  "type": "string"
                }
              },
              "required": [
                "name"
              ],
              "additionalProperties": {
                "$ref": "#/definitions/User"
              }
            }
          }
        }
      }
    }
  },
  "definitions": {
    "User": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        },
        "tag": {
          "type": "string"
        }
      },
      "required": [
        "name"
      ]
    }
  }
}

The revised response allows extra keys only when their values match User. Apply it if the API supports that extension.

References