How to use apispec - 10 common examples

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
def test_openapi_tools_validate_v2():
    ma_plugin = MarshmallowPlugin()
    spec = APISpec(
        title="Pets", version="0.1", plugins=(ma_plugin,), openapi_version="2.0"
    )
    openapi = ma_plugin.converter

    spec.components.schema("Category", schema=CategorySchema)
    spec.components.schema("Pet", {"discriminator": "name"}, schema=PetSchema)

    spec.path(
        view=None,
        path="/category/{category_id}",
        operations={
            "get": {
                "parameters": [
                    {"name": "q", "in": "query", "type": "string"},
                    {
                        "name": "category_id",
github 4Catalyzer / flask-resty / tests / test_swagger.py View on Github external
def test_definition_autogeneration(views):
    spec = APISpec(
        title='test api',
        version='0.1.0',
        plugins=(FlaskRestyPlugin(),),
    )

    spec.add_path(view=views['foo_list'])

    assert 'FooSchema' in spec.to_dict()['definitions']
github marshmallow-code / apispec / tests / test_ext_marshmallow.py View on Github external
def test_schema_uses_ref_if_available_name_resolver_returns_none_v2(self):
        def resolver(schema):
            return None

        spec = APISpec(
            title="Test auto-reference",
            version="0.1",
            openapi_version="2.0",
            plugins=(MarshmallowPlugin(schema_name_resolver=resolver),),
        )
        spec.components.schema("Pet", schema=PetSchema)
        spec.path(
            path="/pet", operations={"get": {"responses": {200: {"schema": PetSchema}}}}
        )
        get = get_paths(spec)["/pet"]["get"]
        assert get["responses"]["200"]["schema"] == build_ref(spec, "schema", "Pet")
github marshmallow-code / apispec / tests / test_ext_combination.py View on Github external
def check_web_framework_and_marshmallow_plugin(web_framework_plugin, **kwargs_for_add_path):
    """Check schemas passed in web framework view function docstring are parsed by MarshmallowPlugin"""
    spec = APISpec(
        title='Swagger Petstore',
        version='1.0.0',
        plugins=[web_framework_plugin(), MarshmallowPlugin()],
        openapi_version='2.0',
    )
    spec.add_path(**kwargs_for_add_path)
    expected = {
        'type': 'object',
        'properties': {
            'id': {'type': 'integer', 'format': 'int32', 'description': 'Pet id', 'readOnly': True},
            'name': {'type': 'string', 'description': 'Pet name'},
        },
        'required': ['name'],
    }
    assert spec.to_dict()['paths']['/hello']['get']['responses'][200]['schema'] == expected
github marshmallow-code / apispec / tests / test_ext_combination.py View on Github external
def check_web_framework_and_marshmallow_plugin(web_framework_plugin, **kwargs_for_add_path):
    """Check schemas passed in web framework view function docstring are parsed by MarshmallowPlugin"""
    spec = APISpec(
        title='Swagger Petstore',
        version='1.0.0',
        plugins=[web_framework_plugin(), MarshmallowPlugin()],
        openapi_version='2.0',
    )
    spec.add_path(**kwargs_for_add_path)
    expected = {
        'type': 'object',
        'properties': {
            'id': {'type': 'integer', 'format': 'int32', 'description': 'Pet id', 'readOnly': True},
            'name': {'type': 'string', 'description': 'Pet name'},
        },
        'required': ['name'],
    }
    assert spec.to_dict()['paths']['/hello']['get']['responses'][200]['schema'] == expected
github marshmallow-code / apispec / tests / test_ext_marshmallow.py View on Github external
def test_schema_uses_ref_if_available_name_resolver_returns_none_v2(self):
        def resolver(schema):
            return None

        spec = APISpec(
            title="Test auto-reference",
            version="0.1",
            openapi_version="2.0",
            plugins=(MarshmallowPlugin(schema_name_resolver=resolver),),
        )
        spec.components.schema("Pet", schema=PetSchema)
        spec.path(
            path="/pet", operations={"get": {"responses": {200: {"schema": PetSchema}}}}
        )
        get = get_paths(spec)["/pet"]["get"]
        assert get["responses"]["200"]["schema"] == build_ref(spec, "schema", "Pet")
github marshmallow-code / apispec / tests / test_ext_marshmallow.py View on Github external
def test_resolve_schema_dict_auto_reference(self, schema):
        def resolver(schema):
            schema_cls = common.resolve_schema_cls(schema)
            return schema_cls.__name__

        spec = APISpec(
            title="Test auto-reference",
            version="0.1",
            openapi_version="2.0",
            plugins=(MarshmallowPlugin(schema_name_resolver=resolver),),
        )
        with pytest.raises(KeyError):
            get_schemas(spec)

        spec.components.schema("analysis", schema=schema)
        spec.path(
            "/test",
            operations={
                "get": {
                    "responses": {
                        "200": {"schema": build_ref(spec, "schema", "analysis")}
                    }
                }
            },
        )
        definitions = get_schemas(spec)
github marshmallow-code / apispec / tests / test_yaml_utils.py View on Github external
def test_load_operations_from_docstring_empty_docstring(docstring):
    assert yaml_utils.load_operations_from_docstring(docstring) == {}
github marshmallow-code / apispec / tests / test_yaml_utils.py View on Github external
def test_load_yaml_from_docstring_empty_docstring(docstring):
    assert yaml_utils.load_yaml_from_docstring(docstring) == {}
github marshmallow-code / apispec / tests / test_core.py View on Github external
def test_path_check_invalid_http_method(self, spec):
        spec.path("/pet/{petId}", operations={"get": {}})
        spec.path("/pet/{petId}", operations={"x-dummy": {}})
        message = "One or more HTTP methods are invalid"
        with pytest.raises(APISpecError, match=message):
            spec.path("/pet/{petId}", operations={"dummy": {}})