Skip to content

Commit

Permalink
Fix checking string swagger definition
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaspavlin committed Feb 7, 2021
1 parent f14e6c0 commit 861efbe
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
32 changes: 17 additions & 15 deletions swagger_unittest/swagger_parser/swagger_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,12 +489,12 @@ def validate_additional_properties(self, valid_response, response):
except Exception:
return False

def validate_definition(self, definition_name: str, dict_to_test: Dict, definition: Dict = None):
def validate_definition(self, definition_name: str, expected_value: Dict, definition: Dict = None):
"""Validate the given dict according to the given definition.
Args:
definition_name: name of the the definition.
dict_to_test: dict to test.
expected_value: dict or scalar value to test.
definition: definition
Returns:
Expand All @@ -507,21 +507,23 @@ def validate_definition(self, definition_name: str, dict_to_test: Dict, definiti

# Check all required in dict_to_test
spec_def = definition or self.specification['definitions'][definition_name]
if not isinstance(dict_to_test, dict):
return False
all_required_keys_present = all(req in dict_to_test.keys() for req in spec_def.get('required', {}))
if 'required' in spec_def and not all_required_keys_present:
return False
if isinstance(expected_value, dict):
all_required_keys_present = all(req in expected_value.keys() for req in spec_def.get('required', {}))
if 'required' in spec_def and not all_required_keys_present:
return False

# Check no extra arg & type
properties_dict = spec_def.get('properties', {})
for key, value in dict_to_test.items():
if value is not None:
if key not in properties_dict: # Extra arg
return False
else: # Check type
if not self._validate_type(properties_dict[key], value):
# Check no extra arg & type
properties_dict = spec_def.get('properties', {})
for key, value in expected_value.items():
if value is not None:
if key not in properties_dict: # Extra arg
return False
else: # Check type
if not self._validate_type(properties_dict[key], value):
return False
else:
# Definition corresponds to scalar value (string for instance)
return self.check_type(expected_value, spec_def['type'])

return True

Expand Down
5 changes: 5 additions & 0 deletions tests/swagger_parser/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ def inline_parser():
return SwaggerParser('data/inline.yaml')


@pytest.fixture
def swagger_enum_parser():
return SwaggerParser('data/enum.yaml')


@pytest.fixture(scope='module',
params=['data/no_properties.yaml',
'data/object_no_schema.yaml',
Expand Down
21 changes: 21 additions & 0 deletions tests/swagger_parser/data/enum.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
swagger: '2.0'
info:
version: '2016-01-19T11:24:50Z'
title: authentication tests
schemes:
- http
paths:
/test:
get:
description: Get something
responses:
'200':
description: OK
schema:
$ref: '#/definitions/my_enum'
definitions:
my_enum:
type: string
enum:
- OPTION_A
- OPTION_B
8 changes: 8 additions & 0 deletions tests/swagger_parser/test_swagger_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ def test_validate_definition(swagger_parser, pet_definition_example):
assert not swagger_parser.validate_definition('Pet', 'foo')


def test_validate_enum_definition(swagger_enum_parser):
# Check string instead of dict_to_test
assert swagger_enum_parser.validate_definition('my_enum', 'OPTION_A')

# TODO: also check for correct enum value (currently enum is treated as string)
assert swagger_enum_parser.validate_definition('my_enum', 'FOO_BAR')


def test_get_paths_data(swagger_parser, post_put_path_data, get_path_data):
swagger_parser.get_paths_data()
assert len(swagger_parser.paths) == 13
Expand Down

0 comments on commit 861efbe

Please sign in to comment.