Skip to content

OAS30Validator readonly writeonly fix #135

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
Oct 5, 2023
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
10 changes: 2 additions & 8 deletions openapi_schema_validator/_keywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,27 +234,21 @@ def additionalProperties(
yield ValidationError(error % extras_msg(extras))


def readOnly(
def write_readOnly(
validator: Validator,
ro: bool,
instance: Any,
schema: Mapping[Hashable, Any],
) -> Iterator[ValidationError]:
if not getattr(validator, "write", True) or not ro:
return

yield ValidationError(f"Tried to write read-only property with {instance}")


def writeOnly(
def read_writeOnly(
validator: Validator,
wo: bool,
instance: Any,
schema: Mapping[Hashable, Any],
) -> Iterator[ValidationError]:
if not getattr(validator, "read", True) or not wo:
return

yield ValidationError(f"Tried to read write-only property with {instance}")


Expand Down
10 changes: 4 additions & 6 deletions openapi_schema_validator/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
"$ref": _keywords.ref,
# fixed OAS fields
"discriminator": oas_keywords.not_implemented,
"readOnly": oas_keywords.readOnly,
"writeOnly": oas_keywords.writeOnly,
"readOnly": oas_keywords.not_implemented,
"writeOnly": oas_keywords.not_implemented,
"xml": oas_keywords.not_implemented,
"externalDocs": oas_keywords.not_implemented,
"example": oas_keywords.not_implemented,
Expand All @@ -68,16 +68,14 @@
OAS30Validator,
validators={
"required": oas_keywords.read_required,
"readOnly": oas_keywords.not_implemented,
"writeOnly": oas_keywords.writeOnly,
"writeOnly": oas_keywords.read_writeOnly,
},
)
OAS30WriteValidator = extend(
OAS30Validator,
validators={
"required": oas_keywords.write_required,
"readOnly": oas_keywords.readOnly,
"writeOnly": oas_keywords.not_implemented,
"readOnly": oas_keywords.write_readOnly,
},
)

Expand Down
10 changes: 10 additions & 0 deletions tests/integration/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,11 @@ def test_read_only(self):
format_checker=oas30_format_checker,
)
assert validator.validate({"some_prop": "hello"}) is None
validator = OAS30Validator(
schema,
format_checker=oas30_format_checker,
)
assert validator.validate({"some_prop": "hello"}) is None

def test_write_only(self):
schema = {
Expand All @@ -617,6 +622,11 @@ def test_write_only(self):
format_checker=oas30_format_checker,
)
assert validator.validate({"some_prop": "hello"}) is None
validator = OAS30Validator(
schema,
format_checker=oas30_format_checker,
)
assert validator.validate({"some_prop": "hello"}) is None

def test_required_read_only(self):
schema = {
Expand Down