Skip to content

Commit 2468a62

Browse files
authored
Merge pull request #26 from Larox/issue24-schema-mutation
[Issue24] Prevent Schema Mutation
2 parents 8a91bfb + 7ed2ea8 commit 2468a62

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ coverage.xml
5050
*.py,cover
5151
.hypothesis/
5252
.pytest_cache/
53+
reports/
5354

5455
# Translations
5556
*.mo

openapi_schema_validator/validators.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from copy import deepcopy
2+
13
from jsonschema import _legacy_validators, _utils, _validators
24
from jsonschema.validators import create
35

@@ -63,7 +65,8 @@ def __init__(self, *args, **kwargs):
6365

6466
def iter_errors(self, instance, _schema=None):
6567
if _schema is None:
66-
_schema = self.schema
68+
# creates a copy by value from schema to prevent mutation
69+
_schema = deepcopy(self.schema)
6770

6871
# append defaults to trigger validator (i.e. nullable)
6972
if 'nullable' not in _schema:

tests/unit/test_shortcut.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from openapi_schema_validator import validate
2+
from unittest import TestCase
3+
4+
5+
class ValidateTest(TestCase):
6+
def test_validate_does_not_mutate_schema_adding_nullable_key(self):
7+
schema = {
8+
"type": "object",
9+
'properties': {
10+
'email': {
11+
'type': 'string'
12+
},
13+
'enabled': {
14+
'type': 'boolean',
15+
}
16+
},
17+
'example': {'enabled': False, 'email': "[email protected]"}
18+
}
19+
20+
validate({"email": "[email protected]"}, schema)
21+
22+
self.assertTrue("nullable" not in schema["properties"]["email"].keys())

0 commit comments

Comments
 (0)