1

我正在尝试将django-rest-swagger==2.1.1与我现有的使用djangorestframework==3.5.3的项目集成。Django Rest Swagger 2:到目前为止有没有为基于FUNCTION视图的POST请求记录参数的文档?

该项目有一些基于类的视图和一些基于功能的视图。在集成swagger之后,它会显示“Class Based views”(显然有序列化程序)的POST请求的输入框,但不会显示“基于功能的视图”。这个问题已经被问了好几次,我曾尝试以下解决方案:

Solution1 Solution2

和其他几个人也一样,但我的情况下,没有工作。是否有任何可能的方式来做'基于功能的视图',或者我必须将它们转换为基于类的视图?

回答

1

在REST中弃用YAML文档字符串解析器Swagger> = 2.0
我所做的是重写SchemaGenerator类以按照我自己的约定来解析视图的文档字符串。

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.schemas import SchemaGenerator 
from rest_framework.views import APIView 

from rest_framework_swagger import renderers 

import yaml 
import coreapi 
import urlparse 

class SchemaGenerator(SchemaGenerator): 
    def get_link(self, path, method, view): 
     """Custom the coreapi using the func.__doc__ . 

     if __doc__ of the function exist, use the __doc__ building the coreapi. else use the default serializer. 

     __doc__ in yaml format, eg: 

     description: the desc of this api. 
     parameters: 
      - name: mobile 
       desc: the mobile number 
       type: string 
       required: true 
       location: form 
      - name: promotion 
       desc: the activity id 
       type: int 
       required: true 
       location: form 
     """ 
     fields = self.get_path_fields(path, method, view) 
     yaml_doc = None 
     if view and view.__doc__: 
      try: 
       yaml_doc = yaml.load(view.__doc__) 
      except: 
       yaml_doc = None 

     if yaml_doc and type(yaml_doc) != str: 
      _method_desc = yaml_doc.get('description', '') 
      params = yaml_doc.get('parameters', []) 
      for i in params: 
       _name = i.get('name') 
       _desc = i.get('description') 
       _required = i.get('required', False) 
       _type = i.get('type', 'string') 
       _location = i.get('location', 'form') 
       field = coreapi.Field(
        name=_name, 
        location=_location, 
        required=_required, 
        description=_desc, 
        type=_type 
       ) 
       fields.append(field) 
     else: 
      _method_desc = view.__doc__ if view and view.__doc__ else '' 
      fields += self.get_serializer_fields(path, method, view) 
     fields += self.get_pagination_fields(path, method, view) 
     fields += self.get_filter_fields(path, method, view) 

     if fields and any([field.location in ('form', 'body') for field in fields]): 
      encoding = self.get_encoding(path, method, 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, 
      description=_method_desc 
     ) 

def get_swagger_view(title=None, url=None, patterns=None, urlconf=None): 
    """ 
    Returns schema view which renders Swagger/OpenAPI. 
    """ 
    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 = SchemaGenerator(
       title=title, 
       url=url, 
       patterns=patterns, 
       urlconf=urlconf 
      ) 
      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() 

在项目结构的任何位置创建此模块。从project/urls.py这个模块导入get_swagger_view。然后,从django_rest_swagger模块中删除get_swagger_view方法。

编号:Comment by daimon99 in REST Swagger Issues

+0

全面的答案,但有与样品YAML有点问题的是你的答案显示'int'不起作用,我用字符串取代它,它运行良好,对此有何评论? –

+0

哦。我从来没有真正测试过“类型”。谢谢你这么说。我会检查可能是什么问题。 –

0

您可以使用装饰:

from rest_framework.decorators import api_view 

,然后上面的功能用途:

@api_view(['POST']) 
相关问题