Add suport for Response schemas (#10)
Schemas generated from Serializers will now be added to the `definitions` section by default, and used as `$ref` objects where needed. The Schema definition name is based on the serializer class name, and can be overriden by specifying a `__ref_name__` property on the Serializer. If this property is set to None, the schema will not be added to `definitions` and will be forced inline. Closes #6, #7.
This commit is contained in:
+18
-5
@@ -6,30 +6,43 @@ from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
|
||||
from drf_swagger import openapi
|
||||
from drf_swagger.utils import swagger_auto_schema
|
||||
from drf_swagger.utils import swagger_auto_schema, no_body
|
||||
from users.serializers import UserSerializer
|
||||
|
||||
|
||||
class UserList(APIView):
|
||||
"""UserList cbv classdoc"""
|
||||
|
||||
@swagger_auto_schema(responses={200: UserSerializer(many=True)})
|
||||
def get(self, request):
|
||||
queryset = User.objects.all()
|
||||
serializer = UserSerializer(queryset, many=True)
|
||||
return Response(serializer.data)
|
||||
|
||||
@swagger_auto_schema(request_body=UserSerializer, operation_description="apiview post description override")
|
||||
@swagger_auto_schema(operation_description="apiview post description override", request_body=openapi.Schema(
|
||||
type=openapi.TYPE_OBJECT,
|
||||
required=['username'],
|
||||
properties={
|
||||
'username': openapi.Schema(type=openapi.TYPE_STRING)
|
||||
},
|
||||
))
|
||||
def post(self, request):
|
||||
serializer = UserSerializer(request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
serializer.save()
|
||||
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||
|
||||
@swagger_auto_schema(request_body=no_body, operation_description="dummy operation")
|
||||
def patch(self, request):
|
||||
pass
|
||||
|
||||
|
||||
@swagger_auto_schema(method='put', request_body=UserSerializer)
|
||||
@swagger_auto_schema(method='get', manual_parameters=[
|
||||
openapi.Parameter('test', openapi.IN_QUERY, "test manual param", type=openapi.TYPE_BOOLEAN)
|
||||
])
|
||||
@swagger_auto_schema(methods=['get'], manual_parameters=[
|
||||
openapi.Parameter('test', openapi.IN_QUERY, "test manual param", type=openapi.TYPE_BOOLEAN),
|
||||
], responses={
|
||||
200: openapi.Response('response description', UserSerializer),
|
||||
})
|
||||
@api_view(['GET', 'PUT'])
|
||||
def user_detail(request, pk):
|
||||
"""user_detail fbv docstring"""
|
||||
|
||||
Reference in New Issue
Block a user