Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_flex_validate_failure():
with pytest.raises(ValidationError):
parser = BaseParser('tests/specs/missing_reference.yaml', backend = 'flex')
def test_openapi_spec_validator_validate_failure():
with pytest.raises(ValidationError):
parser = BaseParser('tests/specs/missing_reference.yaml', backend = 'openapi-spec-validator')
def test_convert_spec():
from prance import BaseParser, ResolvingParser, ValidationError
parser = BaseParser('tests/specs/petstore.yaml')
# Conversion should fail with the default backend.
with pytest.raises(ValidationError):
converted = convert.convert_spec(parser.specification)
# However, with the lazy flag it should work.
converted = convert.convert_spec(parser.specification, lazy = True)
assert isinstance(converted, BaseParser)
# Passing a ResolvingParser class should also work.
converted = convert.convert_spec(parser.specification, ResolvingParser, lazy = True)
assert isinstance(converted, ResolvingParser)
def test_flex_issue_5_integer_keys():
# Must succeed with default (flex) parser; note the parser does not stringify the response code
parser = BaseParser('tests/specs/issue_5.yaml', backend = 'flex')
assert 200 in parser.specification['paths']['/test']['post']['responses']
def test_swagger_spec_validator_validate_failure():
with pytest.raises(ValidationError):
parser = BaseParser('tests/specs/missing_reference.yaml', backend = 'swagger-spec-validator')
def test_openapi_spec_validator_issue_20_spec_version_handling():
# The spec is OpenAPI 3, but broken. Need to set 'strict' to False to stringify keys
with pytest.raises(ValidationError):
parser = BaseParser('tests/specs/issue_20.yaml', backend = 'openapi-spec-validator', strict = False)
# Lazy parsing should let us validate what's happening
parser = BaseParser('tests/specs/issue_20.yaml', backend = 'openapi-spec-validator', strict = False, lazy = True)
assert not parser.valid
assert parser.version_parsed == ()
with pytest.raises(ValidationError):
parser.parse()
# After parsing, the specs are not valid, but the correct version is
# detected.
assert not parser.valid
assert parser.version_parsed == (3, 0, 0)
def test_swagger_spec_validator_issue_5_integer_keys():
# Must fail in implicit strict mode.
with pytest.raises(ValidationError):
BaseParser('tests/specs/issue_5.yaml', backend = 'swagger-spec-validator')
# Must fail in explicit strict mode.
with pytest.raises(ValidationError):
BaseParser('tests/specs/issue_5.yaml', backend = 'swagger-spec-validator', strict = True)
# Must succeed in non-strict/lenient mode
parser = BaseParser('tests/specs/issue_5.yaml', backend = 'swagger-spec-validator', strict = False)
assert '200' in parser.specification['paths']['/test']['post']['responses']
def test_convert_parser_lazy_swagger_backend():
from prance import BaseParser, ResolvingParser, ValidationError
parser = BaseParser('tests/specs/petstore.yaml')
# Conversion should fail with the default backend.
with pytest.raises(ValidationError):
converted = convert.convert_spec(parser)
# However, with the lazy flag it should work.
converted = convert.convert_spec(parser, lazy = True)
assert isinstance(converted, BaseParser)
# Passing a ResolvingParser class should also work.
converted = convert.convert_spec(parser, ResolvingParser, lazy = True)
assert isinstance(converted, ResolvingParser)
:raise: apispec.exceptions.OpenAPIError if validation fails.
"""
try:
import prance
except ImportError as error: # re-raise with a more verbose message
exc_class = type(error)
raise exc_class(
"validate_spec requires prance to be installed. "
"You can install all validation requirements using:\n"
" pip install 'apispec[validation]'"
)
parser_kwargs = {}
if spec.openapi_version.version[0] == 3:
parser_kwargs["backend"] = "openapi-spec-validator"
try:
prance.BaseParser(spec_string=json.dumps(spec.to_dict()), **parser_kwargs)
except prance.ValidationError as err:
raise exceptions.OpenAPIError(*err.args)
else:
return True