I made an API and want to make swagger doc. I don\'t develop any Serializes for that.
Views.py
class DeliveryView(APIVie
Consider using the GenericAPIView
instead as this will generate the documentation. It is a bit of a hack using this when the endpoint does not relate to a model, but it does work.
As an example, the following code will create an endpoint that only accepts post requests and is documented within swagger using the seralizer.
class SomeThing(GenericAPIView):
serializer_class = MySerializer
def post(self, request, *args, **kwargs):
serializer = MySerializer(data=request.data)
if serializer.is_valid() is False:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
res = do_magic(**serializer.data)
return Response(res)
The get_swagger_view()
method does not give you the control to add parameters and descriptions to the urls in your app
The solution for this is to use explicit schema definition. You can create a Document which is a representation of the Core API schema container. Go through the following link. Read the Core API section
Core API schema generator
This will require you to create a schema for the urls and the parameters that are used in your Application.
Create a file for swagger
swagger.py
from rest_framework.decorators import renderer_classes, api_view
from rest_framework_swagger.renderers import OpenAPIRenderer, SwaggerUIRenderer
import coreapi
from rest_framework import response
# noinspection PyArgumentList
@api_view()
@renderer_classes([SwaggerUIRenderer, OpenAPIRenderer])
def schema_view(request):
print("---inside schema view-----")
# noinspection PyArgumentList
schema = coreapi.Document(
title='Your Title',
url='Your host url',
content={
'search': coreapi.Link(
url='/search/',
action='get',
fields=[
coreapi.Field(
name='from',
required=True,
location='query',
description='City name or airport code.'
),
coreapi.Field(
name='to',
required=True,
location='query',
description='City name or airport code.'
),
coreapi.Field(
name='date',
required=True,
location='query',
description='Flight date in "YYYY-MM-DD" format.'
)
],
description='Return flight availability and prices.'
)
}
)
# schema = generator.get_schema(request)
return response.Response(schema)
For explanation on coreapi.Document
, coreapi.Field
, coreapi.Link
kindly refer to the above mentioned link.
Create the url for the swagger docs:
urls.py
url('^docs', swagger.schema_view)