Skip to content
This repository has been archived by the owner on Nov 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #300 from arttuperala/anyof-nullable
Browse files Browse the repository at this point in the history
Check anyOf and oneOf schemas in nullable validation
  • Loading branch information
sondrelg authored Jul 4, 2023
2 parents b2aa34e + 7eb51c0 commit 8cd209a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions openapi_tester/schema_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,20 @@ def test_is_nullable(schema_item: dict) -> bool:
"""
openapi_schema_3_nullable = "nullable"
swagger_2_nullable = "x-nullable"
if "oneOf" in schema_item:
one_of: list[dict[str, Any]] = schema_item.get("oneOf", [])
return any(
nullable_key in schema and schema[nullable_key]
for schema in one_of
for nullable_key in [openapi_schema_3_nullable, swagger_2_nullable]
)
if "anyOf" in schema_item:
any_of: list[dict[str, Any]] = schema_item.get("anyOf", [])
return any(
nullable_key in schema and schema[nullable_key]
for schema in any_of
for nullable_key in [openapi_schema_3_nullable, swagger_2_nullable]
)
return any(
nullable_key in schema_item and schema_item[nullable_key]
for nullable_key in [openapi_schema_3_nullable, swagger_2_nullable]
Expand Down
14 changes: 14 additions & 0 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,17 @@ def test_byte_validation():

with pytest.raises(DocumentationError):
tester.test_schema_section({"type": "string", "format": "byte"}, "test123")


def test_is_nullable_anyof():
tester.test_schema_section({"anyOf": [{"type": "object", "nullable": True}, {"type": "string"}]}, None)

with pytest.raises(DocumentationError):
tester.test_schema_section({"anyOf": [{"type": "object"}, {"type": "string"}]}, None)


def test_is_nullable_oneof():
tester.test_schema_section({"oneOf": [{"type": "object", "nullable": True}, {"type": "string"}]}, None)

with pytest.raises(DocumentationError):
tester.test_schema_section({"oneOf": [{"type": "object"}, {"type": "string"}]}, None)

0 comments on commit 8cd209a

Please sign in to comment.