Skip to content

oas30validator subclassing workaround #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions openapi_schema_validator/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from openapi_schema_validator import _validators as oas_validators
from openapi_schema_validator._types import oas31_type_checker

BaseOAS30Validator = create(
OAS30Validator = create(
meta_schema=_utils.load_schema("draft4"),
validators={
u"multipleOf": _validators.multipleOf,
Expand Down Expand Up @@ -76,17 +76,19 @@
)


@attrs
class OAS30Validator(BaseOAS30Validator):
def _patch_validator_with_read_write_context(cls):
"""Adds read/write context to jsonschema validator class"""
# subclassing validator classes is not intended to
# be part of their public API and will raise error
# See https://github.com/p1c2u/openapi-schema-validator/issues/48
original_init = cls.__init__

read: bool = attrib(default=None)
write: bool = attrib(default=None)
def __init__(self, *args, **kwargs):
self.read = kwargs.pop("read", None)
self.write = kwargs.pop("write", None)
original_init(self, *args, **kwargs)

@classmethod
def __init_subclass__(cls):
# Subclassing validator classes is not intended to
# be part of their public API
# but it's the only way to pass extra context
# to the validator
# See https://github.com/p1c2u/openapi-schema-validator/issues/48
pass
cls.__init__ = __init__


_patch_validator_with_read_write_context(OAS30Validator)