python-openapi/openapi-schema-validator

Failure to correctly validate required in allOf

sebastianmika opened this issue · 2 comments

Having one spec with optional properties and creating a derived spec using allOf that makes a property required fails. This should be supported according to json schema itself (e.g. here)

To reproduce (with 0.1.5):

from openapi_schema_validator import validate


schema = {
    "allOf": [
        {"type": "object", "properties": {"some_prop": {"type": "string"}}},
        {"type": "object", "required": ["some_prop"]},
    ]
}

validate({"another_prop": "bla"}, schema)

Expected behaviour: an error that the required property some_prop is missing. The code actually correctly figures that out, but when it tries to check whether it is ok to have that property missing because it is e.g. a readOnly missing in a write operation a "runtime" error is produced instead of a validation exception:

~/venvs/py39/lib/python3.9/site-packages/openapi_schema_validator/_validators.py in required(validator, required, instance, schema)
     41     for property in required:
     42         if property not in instance:
---> 43             prop_schema = schema['properties'][property]
     44             read_only = prop_schema.get('readOnly', False)
     45             write_only = prop_schema.get('writeOnly', False)

KeyError: 'properties'

The problem is that here schema is only the second schema with the required - but the property can come from any schema in the allOf.

Ok - looking closer I would say the root cause is the same as in #14

p1c2u commented

Fix merged hence closing