Exemple #1
0
    def schema_class_resolver(self, main_plugin: SerpycoPlugin) -> SchemaUsage:
        """
        Return schema class with adaptation if needed.
        :param main_plugin: Apispec plugin associated for marshmallow
        :return: schema generated from given schema or original schema if
            no change required.
        """
        serpyco_plugin_kwargs = {}
        serpyco_name_resolver_kwargs = {}

        if self._exclude:
            serpyco_plugin_kwargs["exclude"] = self._exclude
            serpyco_name_resolver_kwargs["exclude"] = self._exclude

        if self._only:
            serpyco_plugin_kwargs["only"] = self._only
            serpyco_name_resolver_kwargs["only"] = self._only

        return SchemaUsage(
            self.schema,
            plugin_helper_kwargs={
                "serpyco_builder_args": serpyco_plugin_kwargs
            },
            plugin_name_resolver_kwargs=serpyco_name_resolver_kwargs,
        )
Exemple #2
0
 def schema_class_resolver(
         self, main_plugin: MarshmallowAdvancedPlugin) -> SchemaUsage:
     """
     Return schema class with adaptation if needed.
     :param main_plugin: Apispec plugin associated for marshmallow
     :return: schema generated from given schema or original schema if
         no change required.
     """
     return SchemaUsage(schema_class_resolver_(main_plugin, self.schema))
Exemple #3
0
    def get_doc(
        self,
        hapic: "Hapic",
        controllers: typing.List[DecoratedController],
        context: ContextInterface,
        title: str = "",
        description: str = "",
        version: str = "1.0.0",
        wildcard_method_replacement: typing.Optional[
            typing.List["str"]] = None,
    ) -> dict:
        """
        Generate an OpenApi 2.0 documentation. Th given context will be used
        to found controllers matching with given DecoratedController.
        :param controllers: List of DecoratedController to match with context
        controllers
        :param context: a context instance
        :param title: The generated doc title
        :param description: The generated doc description
        :param wildcard_method_replacement: If wild card found as method in
        operations consider these given methods as replacement. If not provided
        all OpenAPI v2 valid methods will be used.
        :return: a apispec documentation dict
        """
        main_plugin = hapic.processor_class.create_apispec_plugin()

        plugins = (main_plugin, )
        spec = APISpec(
            title=title,
            info=dict(description=description),
            version=version,
            plugins=plugins,
            openapi_version="2.0",
        )

        schema_usages = []  # type: typing.List[SchemaUsage]
        # Parse used schema and pre-index them into apispec plugin
        for controller in controllers:
            description = controller.description

            for description_item in [
                    description.input_body,
                    description.input_path,
                    description.input_query,
                    description.input_forms,
                    description.output_body,
            ]:
                if description_item:
                    schema_usage = description_item.wrapper.processor.schema_class_resolver(
                        main_plugin)
                    schema_usages.append(schema_usage)

            if description.errors:
                for error in description.errors:
                    error_schema = error.wrapper.error_builder.get_schema()
                    schema_usages.append(SchemaUsage(error_schema))

        for schema_usage in set(schema_usages):
            try:
                spec.components.schema(
                    main_plugin.schema_name_resolver(
                        schema_usage.schema,
                        **schema_usage.plugin_name_resolver_kwargs),
                    schema=schema_usage.schema,
                    **schema_usage.plugin_helper_kwargs,
                )
            except DuplicateComponentNameError:
                pass  # Already registered schema

        # add views
        for controller in controllers:
            if controller.description.disable_doc:
                continue

            route = context.find_route(controller)
            swagger_path = context.get_swagger_path(route.rule)

            operations = generate_operations(main_plugin, route,
                                             controller.description)

            # Special cases compliance by replacing "*" by acceptable methods (apispec crash
            # because OpenAPI only accepted valid http method)
            if "*" in operations:
                operation_value = operations["*"]
                del operations["*"]
                wildcard_method_replacement = (wildcard_method_replacement
                                               or VALID_METHODS_OPENAPI_V2)
                for method in wildcard_method_replacement:
                    operations[method] = operation_value

            doc_string = controller.reference.get_doc_string()
            if doc_string:
                for method in operations.keys():
                    operations[method]["description"] = doc_string

            spec.path(swagger_path, operations=operations)

        return spec.to_dict()