Skip to content
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

Add type check when validating definition #136

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions swagger_spec_validator/validator20.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,10 @@ def validate_definition(definition, deref, def_name=None, visited_definitions_id
return
visited_definitions_ids.add(id(definition))

if not isinstance(definition, dict):
raise SwaggerValidationError('Definition of {} must be a dict; got {}'.format(
def_name or '(no name)', definition.__class__.__name__))

swagger_type = definition.get('type')
if isinstance(swagger_type, list):
# not valid Swagger; see https://github.com/OAI/OpenAPI-Specification/issues/458
Expand Down
12 changes: 12 additions & 0 deletions tests/validator20/validate_definitions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ def test_type_array_without_items_succeed_fails():
assert str(excinfo.value) == 'Definition of type array must define `items` property (definition #/definitions/definition_1).'


def test_non_dict_fails_with_informative_error_message():
definitions = {
'Example': 'invalid',
}

with pytest.raises(
SwaggerValidationError,
match=r"Definition of #/definitions/Example must be a dict; got .*",
):
validate_definitions(definitions, lambda x: x)


def test_inline_model_is_not_valid_validation_fails():
definitions = {
'definition_1': {
Expand Down