How to use the apispec.exceptions function in apispec

To help you get started, we’ve selected a few apispec examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github marshmallow-code / apispec / tests / test_ext_marshmallow_openapi.py View on Github external
{
                            "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))
github marshmallow-code / apispec / tests / test_ext_marshmallow_openapi.py View on Github external
),
                "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))
github marshmallow-code / apispec / src / apispec / utils.py View on Github external
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)
github marshmallow-code / apispec / src / apispec / utils.py View on Github external
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
github marshmallow-code / apispec / apispec / utils.py View on Github external
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'))