def test_swagger_generator_v2(registry, swagger_version, expected_swagger):
    if swagger_version == 2:
        swagger_jsonschema = SWAGGER_V2_JSONSCHEMA
        generator = SwaggerV2Generator()
    elif swagger_version == 3:
        swagger_jsonschema = SWAGGER_V3_JSONSCHEMA
        generator = SwaggerV3Generator()
    else:
        raise ValueError("Unknown swagger_version: {}".format(swagger_version))

    validate_swagger(expected_swagger, schema=swagger_jsonschema)

    swagger = generator.generate(registry)

    result = json.dumps(swagger, indent=2, sort_keys=True)
    expected = json.dumps(expected_swagger, indent=2, sort_keys=True)

    assert result == expected
예제 #2
0
import marshmallow as m

from flask_rebar import Rebar
from flask_rebar import HeaderApiKeyAuthenticator
from flask_rebar import compat
from flask_rebar.swagger_generation import SwaggerV2Generator, SwaggerV3Generator

rebar = Rebar()
registry = rebar.create_handler_registry()

swagger_v2_generator = SwaggerV2Generator()
swagger_v3_generator_with_hidden = SwaggerV3Generator(include_hidden=True)
normal_swagger_v3_generator = SwaggerV3Generator()

authenticator = HeaderApiKeyAuthenticator(header="x-auth")
default_authenticator = HeaderApiKeyAuthenticator(header="x-another", name="default")


class HeaderSchema(m.Schema):
    user_id = compat.set_data_key(field=m.fields.String(required=True), key="X-UserId")


class FooSchema(m.Schema):
    __swagger_title__ = "Foo"

    uid = m.fields.String()
    name = m.fields.String()


class NestedFoosSchema(m.Schema):
    data = m.fields.Nested(FooSchema, many=True)
def test_swagger_v3_generator_non_registry_parameters():
    title = "Test API"
    version = "3.0.0"
    description = "testing testing 123"

    class Error(m.Schema):
        message = m.fields.String()
        details = m.fields.Dict()

    generator = SwaggerV3Generator(
        version=version,
        title=title,
        description=description,
        default_response_schema=Error(),
        tags=[
            Tag(
                name="bar",
                description="baz",
                external_docs=ExternalDocumentation(url="http://bardocs.com",
                                                    description="qux"),
            )
        ],
        servers=[
            Server(
                url="https://{username}.gigantic-server.com:{port}/{basePath}",
                description="The production API server",
                variables={
                    "username":
                    ServerVariable(
                        default="demo",
                        description=
                        "this value is assigned by the service provider, in this example `gigantic-server.com`",
                    ),
                    "port":
                    ServerVariable(default="8443", enum=["8443", "443"]),
                    "basePath":
                    ServerVariable(default="v2"),
                },
            )
        ],
    )

    rebar = Rebar()
    registry = rebar.create_handler_registry()

    swagger = generator.generate(registry)

    expected_swagger = {
        "openapi":
        "3.0.2",
        "info": {
            "title": title,
            "version": version,
            "description": description
        },
        "tags": [{
            "name": "bar",
            "description": "baz",
            "externalDocs": {
                "url": "http://bardocs.com",
                "description": "qux"
            },
        }],
        "servers": [{
            "url": "https://{username}.gigantic-server.com:{port}/{basePath}",
            "description": "The production API server",
            "variables": {
                "username": {
                    "default":
                    "demo",
                    "description":
                    "this value is assigned by the service provider, in this example `gigantic-server.com`",
                },
                "port": {
                    "enum": ["8443", "443"],
                    "default": "8443"
                },
                "basePath": {
                    "default": "v2"
                },
            },
        }],
        "paths": {},
        "components": {
            "schemas": {
                "Error": {
                    "type": "object",
                    "title": "Error",
                    "properties": {
                        "message": {
                            "type": "string"
                        },
                        "details": {
                            "type": "object"
                        },
                    },
                }
            }
        },
    }

    validate_swagger(expected_swagger, SWAGGER_V3_JSONSCHEMA)
    _assert_dicts_equal(swagger, expected_swagger)
                        "details": {
                            "type": "object"
                        },
                    },
                }
            }
        },
    }

    validate_swagger(expected_swagger, SWAGGER_V3_JSONSCHEMA)
    _assert_dicts_equal(swagger, expected_swagger)


@pytest.mark.parametrize(
    "generator",
    [SwaggerV2Generator(), SwaggerV3Generator()])
def test_path_parameter_types_must_be_the_same_for_same_path(generator):
    rebar = Rebar()
    registry = rebar.create_handler_registry()

    @registry.handles(rule="/foos/<string:foo_uid>", method="GET")
    def get_foo(foo_uid):
        pass

    @registry.handles(rule="/foos/<int:foo_uid>", method="PATCH")
    def update_foo(foo_uid):
        pass

    with pytest.raises(ValueError):
        generator.generate(registry)
from flask_rebar.authenticators import Authenticator, USE_DEFAULT
from flask_rebar.swagger_generation import SwaggerV2Generator, SwaggerV3Generator
from flask_rebar.swagger_generation.authenticator_to_swagger import (
    AuthenticatorConverterRegistry,
    AuthenticatorConverter,
    HeaderApiKeyConverter,
)

rebar = Rebar()
registry = rebar.create_handler_registry()

authenticator_converter_registry = AuthenticatorConverterRegistry()

swagger_v2_generator = SwaggerV2Generator(
    authenticator_converter_registry=authenticator_converter_registry)
swagger_v3_generator = SwaggerV3Generator(
    authenticator_converter_registry=authenticator_converter_registry)


# If we ever add a HTTP 'Authorization' authenticator then use that.
class FakeHTTPAuthorizationAuthenticator(Authenticator):
    name = "basicAuth"
    schema = "basic"

    def authenticate(self):
        return


class HTTPAuthorizationAuthenticatorConverter(AuthenticatorConverter):

    AUTHENTICATOR_TYPE = FakeHTTPAuthorizationAuthenticator
import marshmallow

from flask_rebar import Rebar
from flask_rebar import RequestSchema
from flask_rebar.validation import QueryParamList
from flask_rebar.swagger_generation import SwaggerV2Generator, SwaggerV3Generator

rebar = Rebar()
registry = rebar.create_handler_registry()

swagger_v2_generator = SwaggerV2Generator()
swagger_v3_generator = SwaggerV3Generator()


class ExplodedQueryStringSchema(RequestSchema):
    foos = QueryParamList(marshmallow.fields.String(),
                          required=True,
                          description="foo string")


@registry.handles(rule="/foos",
                  method="GET",
                  query_string_schema=ExplodedQueryStringSchema())
def get_foos():
    pass


EXPECTED_SWAGGER_V2 = {
    "swagger": "2.0",
    "host": "localhost",
    "consumes": ["application/json"],