Skip to content

OAS30Validator evaluates sibling keywords alongside $ref instead of ignoring them per OpenAPI 3.0 §4.7.24 #304

Open

Description

@josephaxisa

Description

In OpenAPI 3.0.x (§4.7.24 Reference Object and JSON Schema Draft 4 / json-reference), a Reference Object ({"$ref": "..."}) cannot be extended with additional properties and any sibling keywords added alongside $ref SHALL be ignored:

This object cannot be extended with additional properties and any properties added SHALL be ignored.

Currently, _build_oas30_validator() calls jsonschema.validators.create(...) without passing applicable_validators=_legacy_keywords.ignore_ref_siblings (unlike jsonschema.Draft4Validator, which sets applicable_validators=_legacy_keywords.ignore_ref_siblings because create() defaults to evaluating all items as in Draft 2019-09 / 2020-12). In addition, required, read_required, and write_required in _keywords.py inspect prop_schema.get("readOnly") / prop_schema.get("writeOnly") directly on child property schemas even when prop_schema contains "$ref".

As a result, OAS30Validator (and OAS30ReadValidator, OAS30WriteValidator, OAS30StrictValidator) evaluates sibling keywords such as type, nullable, minLength, readOnly, writeOnly, and required when they appear alongside $ref, rather than ignoring them as required by OAS 3.0 (OAS31Validator and OAS32Validator should continue evaluating $ref siblings per JSON Schema Draft 2020-12).

Minimal Reproducer

from openapi_schema_validator import OAS30Validator, validate

schema = {
    "type": "object",
    "required": ["id"],
    "properties": {
        "id": {"type": "integer"},
        "name": {
            "$ref": "#/components/schemas/NullableName",
            "nullable": False,
            "minLength": 10,
        },
    },
    "components": {
        "schemas": {
            "NullableName": {
                "type": "string",
                "nullable": True,
                "minLength": 2,
            }
        }
    },
}

# Raises ValidationError ("None for not nullable" / "'abc' is too short")
# even though `nullable: False` and `minLength: 10` are $ref siblings in OAS 3.0:
validate({"id": 1, "name": None}, schema, cls=OAS30Validator)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions