From 67b0f78d1cefa9955562490d903e24b6dfb07ec5 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Thu, 13 Oct 2022 15:49:43 +0100 Subject: [PATCH 1/4] Add tox file This is the de facto way to run unit tests for most Python projects. The tox-poetry extension is used to provide integration with poetry. Signed-off-by: Stephen Finucane --- tox.ini | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 tox.ini diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..b336870 --- /dev/null +++ b/tox.ini @@ -0,0 +1,11 @@ +[tox] +minversion = 3.18.0 +requires = tox-poetry + +[testenv] +extras = + rfc3339-validator + strict-rfc3339 + isodate +commands = + pytest {posargs:tests/} From be7b8275d3a1b1c1326652d4ed45d62c339bcb9e Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Thu, 13 Oct 2022 16:01:29 +0100 Subject: [PATCH 2/4] README: Correct some typos Signed-off-by: Stephen Finucane --- README.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 5aac52a..edd8891 100644 --- a/README.rst +++ b/README.rst @@ -18,7 +18,7 @@ openapi-schema-validator About ##### -Openapi-schema-validator is a Python library that validates schema against: +*openapi-schema-validator* is a Python library that validates schema against: * `OpenAPI Schema Specification v3.0 `__ which is an extended subset of the `JSON Schema Specification Wright Draft 00 `__. * `OpenAPI Schema Specification v3.1 `__ which is an extended superset of the `JSON Schema Specification Draft 2020-12 `__. @@ -38,7 +38,6 @@ Alternatively you can download the code and install from the repository: $ pip install -e git+https://github.com/p1c2u/openapi-schema-validator.git#egg=openapi_schema_validator - Usage ##### @@ -92,7 +91,7 @@ To validate an OpenAPI v3.1 schema: ... ValidationError: Additional properties are not allowed ('city' was unexpected) -if you want to disambiguate the expected schema version, import and use ``OAS31Validator``: +If you want to disambiguate the expected schema version, import and use ``OAS31Validator``: .. code-block:: python @@ -150,7 +149,7 @@ You can also check format for primitive types References ********** -You can resolve JOSN references by passing custon reference resolver +You can resolve JSON references by passing custom reference resolver .. code-block:: python @@ -204,6 +203,7 @@ For more information about reference resolver see `Resolving JSON References `__ Python library that adds client-side and server-side support for the OpenAPI. * `openapi-spec-validator `__ From 00232d5a11c8fc44503162a1c09f2f4662b13f70 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Thu, 13 Oct 2022 16:01:53 +0100 Subject: [PATCH 3/4] tests: Add tests for nullable object type Signed-off-by: Stephen Finucane --- tests/integration/test_validators.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/integration/test_validators.py b/tests/integration/test_validators.py index 47ea5da..c60ea1c 100644 --- a/tests/integration/test_validators.py +++ b/tests/integration/test_validators.py @@ -21,10 +21,13 @@ class TestOAS30ValidatorValidate: "integer", "number", "string", + "object", ], ) def test_null(self, schema_type): schema = {"type": schema_type} + if schema_type == "object": + schema["properties"] = {"some_prop": {"type": "string"}} validator = OAS30Validator(schema) value = None @@ -39,10 +42,13 @@ def test_null(self, schema_type): "integer", "number", "string", + "object", ], ) def test_nullable(self, schema_type): schema = {"type": schema_type, "nullable": True} + if schema_type == "object": + schema["properties"] = {"some_prop": {"type": "string"}} validator = OAS30Validator(schema) value = None @@ -439,7 +445,7 @@ def test_oneof_discriminator(self, schema_type): ValidationError, match="reference '#/components/schemas/other' could not be resolved", ): - result = validator.validate({"discipline": "other"}) + validator.validate({"discipline": "other"}) assert False @@ -452,10 +458,13 @@ class TestOAS31ValidatorValidate: "integer", "number", "string", + "object", ], ) def test_null(self, schema_type): schema = {"type": schema_type} + if schema_type == "object": + schema["properties"] = {"some_prop": {"type": "string"}} validator = OAS31Validator(schema) value = None @@ -470,10 +479,13 @@ def test_null(self, schema_type): "integer", "number", "string", + "object", ], ) def test_nullable(self, schema_type): schema = {"type": [schema_type, "null"]} + if schema_type == "object": + schema["properties"] = {"some_prop": {"type": "string"}} validator = OAS31Validator(schema) value = None From 964a9163ae22be76ae331ac569d250e84bce391d Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Thu, 13 Oct 2022 16:06:43 +0100 Subject: [PATCH 4/4] 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", [