Skip to content

Commit

Permalink
tests: Add test for nullable refs
Browse files Browse the repository at this point in the history
This is a known bug. Add a test for it so we can work on fixing it.

Signed-off-by: Stephen Finucane <stephen@that.guru>
Related-bug: python-openapi#20
  • Loading branch information
stephenfin committed Oct 14, 2022
1 parent 00232d5 commit 964a916
Showing 1 changed file with 85 additions and 1 deletion.
86 changes: 85 additions & 1 deletion tests/integration/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,54 @@ def test_string_uuid(self, value):

assert result is None

def test_ref(self):
schema = {
"$ref": "#/$defs/Pet",
"$defs": {
"Pet": {
"required": ["id", "name"],
"properties": {
"id": {"type": "integer", "format": "int64"},
"name": {"type": "string"},
"tag": {"type": "string"},
},
}
},
}
validator = OAS30Validator(schema, format_checker=oas30_format_checker)

result = validator.validate({"id": 1, "name": "John"})
assert result is None

with pytest.raises(ValidationError) as excinfo:
validator.validate({"name": "John"})

error = "'id' is a required property"
assert error in str(excinfo.value)

@pytest.mark.xfail(reason="Issue #20")
def test_ref_nullable(self):
schema = {
"nullable": True,
"allOf": [
{
"$ref": "#/$defs/Pet",
},
],
"$defs": {
"Pet": {
"required": ["id", "name"],
"properties": {
"id": {"type": "integer", "format": "int64"},
"name": {"type": "string"},
"tag": {"type": "string"},
},
}
},
}
validator = OAS30Validator(schema, format_checker=oas30_format_checker)
validator.validate(None)

def test_allof_required(self):
schema = {
"allOf": [
Expand All @@ -217,6 +265,20 @@ def test_allof_required(self):
):
validator.validate({"another_prop": "bla"})

@pytest.mark.xfail(reason="Issue #20")
def test_allof_nullable(self):
schema = {
"allOf": [
{
"type": "object",
"properties": {"some_prop": {"type": "string"}},
},
{"type": "object", "nullable": True},
]
}
validator = OAS30Validator(schema, format_checker=oas30_format_checker)
validator.validate(None)

def test_required(self):
schema = {
"type": "object",
Expand Down Expand Up @@ -665,7 +727,7 @@ def test_schema_validation(self):
error = "'-12' is not a 'date'"
assert error in str(excinfo.value)

def test_schema_ref(self):
def test_ref(self):
schema = {
"$ref": "#/$defs/Pet",
"$defs": {
Expand Down Expand Up @@ -693,6 +755,28 @@ def test_schema_ref(self):
error = "'id' is a required property"
assert error in str(excinfo.value)

def test_ref_nullable(self):
# specifying an array for type only works with primitive types
schema = {
"oneOf": [
{"type": "null"},
{"$ref": "#/$defs/Pet"},
],
"$defs": {
"Pet": {
"type": "object",
"required": ["id", "name"],
"properties": {
"id": {"type": "integer", "format": "int64"},
"name": {"type": "string"},
"tag": {"type": "string"},
},
}
},
}
validator = OAS31Validator(schema, format_checker=oas30_format_checker)
validator.validate(None)

@pytest.mark.parametrize(
"value",
[
Expand Down

0 comments on commit 964a916

Please sign in to comment.