Skip to content

Commit 2234b93

Browse files
committed
Update validate function that mutated the schema
1 parent ddb8291 commit 2234b93

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

openapi_schema_validator/validators.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
from jsonschema import _legacy_validators, _utils, _validators
23
from jsonschema.validators import create
34

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

6465
def iter_errors(self, instance, _schema=None):
6566
if _schema is None:
66-
_schema = self.schema
67+
# creates a copy by value from schema to prevent mutation
68+
_schema = copy.deepcopy(self.schema)
6769

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

tests/unit/__init__.py

Whitespace-only changes.

tests/unit/test_shortcut.py

Lines changed: 22 additions & 0 deletions
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)