Esempio n. 1
0
    def test_format_path(self):
        res, args = format_path_for_swagger(
            "/projects/<uuid:project_uid>/foos/<foo_uid>")

        self.assertEqual(res, "/projects/{project_uid}/foos/{foo_uid}")

        self.assertEqual(
            args,
            (
                PathArgument(name="project_uid", type="uuid"),
                PathArgument(name="foo_uid", type="string"),
            ),
        )
Esempio n. 2
0
    def test_no_args(self):
        res, args = format_path_for_swagger("/health")

        self.assertEqual(res, "/health")
        self.assertEqual(args, tuple())
    def _get_paths(self, paths, default_headers_schema, default_security=None):
        path_definitions = {}

        for path, methods in paths.items():
            swagger_path, path_args = format_path_for_swagger(path)

            # Different Flask paths might correspond to the same Swagger path
            # because of Flask URL path converters. In this case, let's just
            # work off the same path definitions.
            if swagger_path in path_definitions:
                path_definition = path_definitions[swagger_path]
            else:
                path_definitions[swagger_path] = path_definition = {}

            if path_args:
                path_params = [{
                    sw.name: path_arg.name,
                    sw.required: True,
                    sw.in_: sw.path,
                    sw.style: sw.simple,
                    sw.schema: {
                        sw.type_:
                        self.flask_converters_to_swagger_types[path_arg.type]
                    },
                } for path_arg in path_args]

                # We have to check for an ugly case here. If different Flask
                # paths that map to the same Swagger path use different URL
                # converters for the same parameter, we have a problem. Let's
                # just throw an error in this case.
                if sw.parameters in path_definition:
                    verify_parameters_are_the_same(
                        path_definition[sw.parameters], path_params)

                path_definition[sw.parameters] = path_params

            for method, d in methods.items():
                responses_definition = {
                    sw.default:
                    self._get_response_definition(self.default_response_schema)
                }

                if d.response_body_schema:
                    for status_code, schema in d.response_body_schema.items():
                        if schema is not None:
                            response_definition = self._get_response_definition(
                                schema)
                        else:
                            response_definition = {
                                sw.description: "No response body."
                            }

                        responses_definition[str(
                            status_code)] = response_definition

                parameters_definition = []

                if d.query_string_schema:
                    parameters_definition.extend(
                        self._convert_schema_to_list_of_parameters(
                            schema=d.query_string_schema,
                            converter=self._query_string_converter,
                            in_=sw.query,
                        ))

                if d.headers_schema is USE_DEFAULT:
                    headers_schema = default_headers_schema
                else:
                    headers_schema = d.headers_schema

                if headers_schema:
                    parameters_definition.extend(
                        self._convert_schema_to_list_of_parameters(
                            schema=headers_schema,
                            converter=self._headers_converter,
                            in_=sw.header,
                        ))

                request_body = None

                if d.request_body_schema:
                    schema = d.request_body_schema

                    request_body = {
                        sw.required: True,
                        sw.content: {
                            "application/json": {
                                sw.schema:
                                get_ref_schema(self._ref_base, schema)
                            }
                        },
                    }

                method_lower = method.lower()
                path_definition[method_lower] = {
                    sw.operation_id: d.endpoint or get_swagger_title(d.func),
                    sw.responses: responses_definition,
                }

                if d.func.__doc__:
                    path_definition[method_lower][
                        sw.description] = d.func.__doc__

                if parameters_definition:
                    path_definition[method_lower][
                        sw.parameters] = parameters_definition

                if request_body:
                    path_definition[method_lower][
                        sw.request_body] = request_body

                if not d.authenticators:
                    path_definition[method_lower][sw.security] = []
                else:
                    non_default = False
                    security = []
                    for authenticator in d.authenticators:
                        if authenticator is not USE_DEFAULT:
                            security.extend(
                                self.authenticator_converter.
                                get_security_requirements(authenticator))
                            non_default = True
                        elif default_security is not None:
                            security.extend(default_security)
                    if non_default:
                        path_definition[method_lower][sw.security] = security

                if d.tags:
                    path_definition[method_lower][sw.tags] = d.tags

        return path_definitions