Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
{
"name": "category_id",
"in": "path",
"required": True,
"type": "string",
}
]
+ openapi.schema2parameters(CategorySchema, default_in="body")
),
"responses": {201: {"schema": PetSchema, "description": "A pet"}},
},
},
)
try:
utils.validate_spec(spec)
except exceptions.OpenAPIError as error:
pytest.fail(str(error))
),
"requestBody": {
"content": {"application/json": {"schema": CategorySchema}}
},
"responses": {
201: {
"description": "created",
"content": {"application/json": {"schema": PetSchema}},
}
},
},
},
)
try:
utils.validate_spec(spec)
except exceptions.OpenAPIError as error:
pytest.fail(str(error))
def __init__(self, openapi_version):
if isinstance(openapi_version, version.LooseVersion):
openapi_version = openapi_version.vstring
if (
not self.MIN_INCLUSIVE_VERSION
<= openapi_version
< self.MAX_EXCLUSIVE_VERSION
):
raise exceptions.APISpecError(
"Not a valid OpenAPI version number: {}".format(openapi_version)
)
super().__init__(openapi_version)
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
def validate_swagger(spec):
"""Validate the output of an :class:`APISpec` object.
Note: Requires installing the node package `swagger-tools`.
:raise: PluginError if validation fails.
"""
with tempfile.NamedTemporaryFile(mode='w') as fp:
json.dump(spec.to_dict(), fp)
fp.seek(0)
try:
subprocess.check_output(
['swagger-tools', 'validate', fp.name],
stderr=subprocess.STDOUT,
)
except subprocess.CalledProcessError as error:
raise exceptions.PluginError(error.output.decode('utf-8'))