Skip to content

Commit 00d2a69

Browse files
authored
Merge pull request #47 from p1c2u/fix/jsonschema-4-10-0-compatibility-fix
jsonschema subclassing fix
2 parents 4d8fd4c + e19f58e commit 00d2a69

File tree

4 files changed

+55
-131
lines changed

4 files changed

+55
-131
lines changed

openapi_schema_validator/_validators.py

+18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
1+
from copy import deepcopy
2+
13
from jsonschema._utils import find_additional_properties, extras_msg
24
from jsonschema._validators import oneOf as _oneOf, anyOf as _anyOf, allOf as _allOf
35

46
from jsonschema.exceptions import ValidationError, FormatError
57

68

9+
def include_nullable_validator(schema):
10+
"""
11+
Include ``nullable`` validator always.
12+
Suitable for use with `create`'s ``applicable_validators`` argument.
13+
"""
14+
_schema = deepcopy(schema)
15+
16+
# append defaults to trigger nullable validator
17+
if 'nullable' not in _schema:
18+
_schema.update({
19+
'nullable': False,
20+
})
21+
22+
return _schema.items()
23+
24+
725
def handle_discriminator(validator, _, instance, schema):
826
"""
927
Handle presence of discriminator in anyOf, oneOf and allOf.

openapi_schema_validator/validators.py

+10-18
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@
5454
# See https://github.com/p1c2u/openapi-schema-validator/pull/12
5555
# version="oas30",
5656
id_of=lambda schema: schema.get(u"id", ""),
57+
applicable_validators=oas_validators.include_nullable_validator,
5758
)
5859

59-
BaseOAS31Validator = extend(
60+
OAS31Validator = extend(
6061
Draft202012Validator,
6162
{
6263
# adjusted to OAS
@@ -81,20 +82,11 @@ class OAS30Validator(BaseOAS30Validator):
8182
read: bool = attrib(default=None)
8283
write: bool = attrib(default=None)
8384

84-
def iter_errors(self, instance, _schema=None):
85-
if _schema is None:
86-
# creates a copy by value from schema to prevent mutation
87-
_schema = deepcopy(self.schema)
88-
89-
# append defaults to trigger validator (i.e. nullable)
90-
if 'nullable' not in _schema:
91-
_schema.update({
92-
'nullable': False,
93-
})
94-
95-
validator = self.evolve(schema=_schema)
96-
return super(OAS30Validator, validator).iter_errors(instance)
97-
98-
99-
class OAS31Validator(BaseOAS31Validator):
100-
pass
85+
@classmethod
86+
def __init_subclass__(cls):
87+
# Subclassing validator classes is not intended to
88+
# be part of their public API
89+
# but it's the only way to pass extra context
90+
# to the validator
91+
# See https://github.com/p1c2u/openapi-schema-validator/issues/48
92+
pass

0 commit comments

Comments
 (0)