python-openapi/openapi-schema-validator

Schema used is mutating inside validation process

Larox opened this issue · 1 comments

Larox commented

Hi all! First of all thanks for providing this tool. It has helped my projects a lot.

I find an issue related to the schema used for the validation. There is an _schema.update(...) in https://github.com/p1c2u/openapi-schema-validator/blob/ab7222af0597ebc672f93cc7f7e089897aba1bec/openapi_schema_validator/validators.py#L68-L72 that is mutating the schema adding nullable: False property in all the fields that doesn't have it.

This schema mutation is generating some issues at the time of using the schema in other features like generating default values and in tests that are expecting the schema without changes.

I'll be happy to work on this and submit a PR.

Steps to reproduce

  • Create a schema that doesn't have the nullable property
  • Use validate(...) function with the instance and the schema
  • Now the schema contains the nullable property

Expected Behaviour

  • The schema shouldn't have changed

Code used as an example for showing the issue

from openapi_schema_validator import validate
schema = {
    "type": "object",
    'properties': {
        'email': {
            'type': 'string'
        },
        'enabled': {
            'type': 'boolean',
        }
    },
    'example': {'enabled': False, 'email': "foo@bar.com"}
}

validate({"email": "foo@bar.com"}, schema)
# schema after the validation
{
    'type': 'object',
    'properties': {
        'email': {
            'type': 'string',
+           'nullable': False
        },
        'enabled': {
            'type': 'boolean',
        }
    },
    'example': {
        'enabled': False,
        'email': "foo@bar.com"
    },
+   'nullable': False
}
p1c2u commented

Fix merged hence closing