Skip to content

Add reproducer for #20 #55

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

Closed
wants to merge 4 commits into from
Closed
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
8 changes: 4 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#schemaObject>`__ which is an extended subset of the `JSON Schema Specification Wright Draft 00 <http://json-schema.org/>`__.
* `OpenAPI Schema Specification v3.1 <https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#schemaObject>`__ which is an extended superset of the `JSON Schema Specification Draft 2020-12 <http://json-schema.org/>`__.
Expand All @@ -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
#####

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -204,6 +203,7 @@ For more information about reference resolver see `Resolving JSON References <ht

Related projects
################

* `openapi-core <https://github.com/p1c2u/openapi-core>`__
Python library that adds client-side and server-side support for the OpenAPI.
* `openapi-spec-validator <https://github.com/p1c2u/openapi-spec-validator>`__
Expand Down
100 changes: 98 additions & 2 deletions tests/integration/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -195,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",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This subschema is invalid (not nullable). That means the whole allof should be invalid isn't it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it? I'd have expected the reference this to flatten to:

{
    "nullable": true,
    "allOf": [
        {
            "required": ["id", "name"],
            "properties": {
                "id": {"type": "integer", "format": "int64"},
                "name": {"type": "string"},
                "tag": {"type": "string"}
            }
        }
    ]
}

which would in-turn flatten to:

{
    "nullable": true,
    "required": ["id", "name"],
    "properties": {
        "id": {"type": "integer", "format": "int64"},
        "name": {"type": "string"},
        "tag": {"type": "string"}
    }
}

Is my understanding incorrect? It might well be! 😄

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, wait, I see your top-level comment now.

allOf can not be used to “extend” a schema to add more details to it in the sense of object-oriented inheritance. Instances must independently be valid against “all of” the schemas in the allOf. See the section on Extending Closed Schemas for more information.

json-schema.org/understanding-json-schema/reference/combining.html#allof

So this being the case, I wonder if the schemas should be outright rejected as invalid?

},
],
"$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 @@ -211,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": [
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This subschema is invalid (not nullable). That means the whole allof should be invalid isn't it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.

"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 @@ -439,7 +507,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


Expand All @@ -452,10 +520,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

Expand All @@ -470,10 +541,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

Expand Down Expand Up @@ -653,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 @@ -681,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
11 changes: 11 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[tox]
minversion = 3.18.0
requires = tox-poetry

[testenv]
extras =
rfc3339-validator
strict-rfc3339
isodate
commands =
pytest {posargs:tests/}