简体   繁体   English

Django rest framework Api文档Swagger 2.0

[英]Django rest framework Api documentation Swagger 2.0

I am having a hard time configuring Swagger UI. 我在配置Swagger UI时遇到了困难。 Here are the very explanatory docs: https://django-rest-swagger.readthedocs.io/en/latest/ 这是非常说明性的文档: https : //django-rest-swagger.readthedocs.io/en/latest/

YAML docstrings are deprecated. 不建议使用YAML文档字符串。 Does somebody know how to configure Swagger UI (query parameters, etc) from within the python code? 有人知道如何从python代码中配置Swagger UI(查询参数等)吗?

If it's impossible for some strange reason. 如果由于某种奇怪的原因不可能。 Is there any working alternative or is it the best for me to just go and write, api documentation by hand? 有什么可行的替代方法,还是最好手动编写api文档?

OK, found it. 好,找到了。 This is not ideal solution - but needed this for frontend (web and mobile) devs - and it do the job. 这不是理想的解决方案-但前端(Web和移动)开发人员需要此解决方案-并且它可以完成工作。

Basically the newest DRF and Swagger uses from rest_framework.schemas import SchemaGenerator for providing the docs for Swagger. 基本上, rest_framework.schemas import SchemaGenerator最新的DRF和Swagger使用rest_framework.schemas import SchemaGenerator为Swagger提供文档。

So I needed to little extend it: 所以我几乎不需要扩展它:

# -*- coding: utf-8 -*-
import urlparse

import coreapi
from rest_framework.schemas import SchemaGenerator


class ParamsSchemaGenerator(SchemaGenerator):

    def get_link(self, path, method, callback, view):
        """
        Return a `coreapi.Link` instance for the given endpoint.
        """
        fields = self.get_path_fields(path, method, callback, view)
        fields += self.get_serializer_fields(path, method, callback, view)
        fields += self.get_pagination_fields(path, method, callback, view)
        fields += self.get_filter_fields(path, method, callback, view)
        fields += self.get_docs_fields(path, method, callback, view)  # this is the extended line;

        if fields and any([field.location in ('form', 'body') for field in fields]):
            encoding = self.get_encoding(path, method, callback, view)
        else:
            encoding = None

        if self.url and path.startswith('/'):
            path = path[1:]

        return coreapi.Link(
            url=urlparse.urljoin(self.url, path),
            action=method.lower(),
            encoding=encoding,
            fields=fields
        )

    # and this is fully custom additional docs method;
    def get_docs_fields(self, path, method, callback, view):
        fields = []
        if hasattr(view, 'docs_fields'):
            for field in view.docs_fields:
                field = coreapi.Field(
                    name=field.get('name'),
                    location=field.get('query'),
                    required=field.get('required'),
                    type=field.get('type'),
                    description=field.get('description')
                )
                fields.append(field)

        return fields

Then i need to define a function that will return the schemas with the generator defined above: 然后,我需要定义一个函数,该函数将使用上面定义的生成器返回架构:

# -*- coding: utf-8 -*-
# monkey patching FTW!

from rest_framework import exceptions
from rest_framework.permissions import AllowAny
from rest_framework.renderers import CoreJSONRenderer
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework_swagger import renderers

from kolomnie.core.schema.generator import ParamsSchemaGenerator


def get_params_swagger_view(title=None, url=None):
    """
    Returns schema view which renders Swagger/OpenAPI.

    (Replace with DRF get_schema_view shortcut in 3.5)
    """
    class SwaggerSchemaView(APIView):
        _ignore_model_permissions = True
        exclude_from_schema = True
        permission_classes = [AllowAny]
        renderer_classes = [
            CoreJSONRenderer,
            renderers.OpenAPIRenderer,
            renderers.SwaggerUIRenderer
        ]

        def get(self, request):
            generator = ParamsSchemaGenerator(title=title, url=url)
            schema = generator.get_schema(request=request)

            if not schema:
                raise exceptions.ValidationError(
                    'The schema generator did not return a schema Document'
                )

            return Response(schema)

    return SwaggerSchemaView.as_view()

This is how i put it in the urls: 这就是我将其放在网址中的方式:

if settings.DEBUG:
    api_views = get_params_swagger_view(title='Some API')

And now little more magic, I defined a mixin for the view which stores the documentation fields: 现在,更神奇的是,我为存储文档字段的视图定义了一个混合:

# -*- coding: utf-8 -*-


class DocsMixin(object):
    """
    This mixin can be used to document the query parameters in GET
    if there's no other way to do it. Please refer to the: ParamsSchemaGenerator.get_links
    for more information;
    """
    docs_fields = []

And this is how I use it: 这就是我的用法:

class BaseSearchResultsView(generics.GenericAPIView, SearchDocs):
    ....

Where SearchDocs is this: 其中SearchDocs是:

class SearchDocs(DocsMixin):
    """
    Documents the get query in search;
    """
    docs_fields = [
        {
            'name': 'q',
            'location': 'query',
            'required': False,
            'type': 'string',
            'description': 'The base query for the search;',
        },
    ...

Just find out that I do not need mixin :) Just docs_fields defined on the view. 只是发现我不需要mixin :)只需在视图上定义docs_fields即可。

Probably this will not fulfill all your needs - but think it's a good start :) 可能这不能满足您的所有需求-但认为这是一个不错的开始:)

Happy coding! 编码愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM