diff --git a/.gitignore b/.gitignore index b6e4761..9ad24b8 100644 --- a/.gitignore +++ b/.gitignore @@ -50,6 +50,7 @@ coverage.xml *.py,cover .hypothesis/ .pytest_cache/ +reports/ # Translations *.mo diff --git a/openapi_schema_validator/validators.py b/openapi_schema_validator/validators.py index 555df89..8521c64 100644 --- a/openapi_schema_validator/validators.py +++ b/openapi_schema_validator/validators.py @@ -1,3 +1,5 @@ +from copy import deepcopy + from jsonschema import _legacy_validators, _utils, _validators from jsonschema.validators import create @@ -63,7 +65,8 @@ def __init__(self, *args, **kwargs): def iter_errors(self, instance, _schema=None): if _schema is None: - _schema = self.schema + # creates a copy by value from schema to prevent mutation + _schema = deepcopy(self.schema) # append defaults to trigger validator (i.e. nullable) if 'nullable' not in _schema: diff --git a/tests/unit/test_shortcut.py b/tests/unit/test_shortcut.py new file mode 100644 index 0000000..9832489 --- /dev/null +++ b/tests/unit/test_shortcut.py @@ -0,0 +1,22 @@ +from openapi_schema_validator import validate +from unittest import TestCase + + +class ValidateTest(TestCase): + def test_validate_does_not_mutate_schema_adding_nullable_key(self): + schema = { + "type": "object", + 'properties': { + 'email': { + 'type': 'string' + }, + 'enabled': { + 'type': 'boolean', + } + }, + 'example': {'enabled': False, 'email': "foo@bar.com"} + } + + validate({"email": "foo@bar.com"}, schema) + + self.assertTrue("nullable" not in schema["properties"]["email"].keys())