From 964a9163ae22be76ae331ac569d250e84bce391d Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Thu, 13 Oct 2022 16:06:43 +0100 Subject: [PATCH] tests: Add test for nullable refs This is a known bug. Add a test for it so we can work on fixing it. Signed-off-by: Stephen Finucane Related-bug: #20 --- tests/integration/test_validators.py | 86 +++++++++++++++++++++++++++- 1 file changed, 85 insertions(+), 1 deletion(-) diff --git a/tests/integration/test_validators.py b/tests/integration/test_validators.py index c60ea1c..bca271d 100644 --- a/tests/integration/test_validators.py +++ b/tests/integration/test_validators.py @@ -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": [ @@ -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", @@ -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": { @@ -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", [