Skip to content

OASValidator 3.0 read write pass with evolve #52

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
Sep 12, 2022
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions openapi_schema_validator/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,21 @@ def _patch_validator_with_read_write_context(cls: Type[Validator]) -> None:
# be part of their public API and will raise error
# See https://github.com/p1c2u/openapi-schema-validator/issues/48
original_init = cls.__init__
original_evolve = cls.evolve

def __init__(self: Validator, *args: Any, **kwargs: Any) -> None:
self.read = kwargs.pop("read", None)
self.write = kwargs.pop("write", None)
original_init(self, *args, **kwargs)

def evolve(self: Validator, **changes: Any) -> Validator:
validator = original_evolve(self, **changes)
validator.read = self.read
validator.write = self.write
return validator

cls.__init__ = __init__
cls.evolve = evolve


_patch_validator_with_read_write_context(OAS30Validator)
36 changes: 36 additions & 0 deletions tests/integration/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,42 @@ def test_required(self):
validator.validate({"another_prop": "bla"})
assert validator.validate({"some_prop": "hello"}) is None

def test_read_only(self):
schema = {
"type": "object",
"properties": {"some_prop": {"type": "string", "readOnly": True}},
}

validator = OAS30Validator(
schema, format_checker=oas30_format_checker, write=True
)
with pytest.raises(
ValidationError, match="Tried to write read-only property with hello"
):
validator.validate({"some_prop": "hello"})
validator = OAS30Validator(
schema, format_checker=oas30_format_checker, read=True
)
assert validator.validate({"some_prop": "hello"}) is None

def test_write_only(self):
schema = {
"type": "object",
"properties": {"some_prop": {"type": "string", "writeOnly": True}},
}

validator = OAS30Validator(
schema, format_checker=oas30_format_checker, read=True
)
with pytest.raises(
ValidationError, match="Tried to read write-only property with hello"
):
validator.validate({"some_prop": "hello"})
validator = OAS30Validator(
schema, format_checker=oas30_format_checker, write=True
)
assert validator.validate({"some_prop": "hello"}) is None

def test_required_read_only(self):
schema = {
"type": "object",
Expand Down