marcgibbons / django-rest-swagger

Swagger Documentation Generator for Django REST Framework: deprecated
https://marcgibbons.com/django-rest-swagger/
BSD 2-Clause "Simplified" License
2.59k stars 602 forks source link

How to support application/json request accept header #701

Open zbyte64 opened 7 years ago

zbyte64 commented 7 years ago

Why is this important? Well it seems the javascript swagger client will always set the accept header to 'application/json`: https://github.com/swagger-api/swagger-js/issues/1124

When the client does the request against the schema url, it responds with: {"detail":"Could not satisfy the request Accept header."}

Since there isn't an apparent resolution on the javascript side, I wanted to pipe application/json to the OpenAPIRenderer ; Sadly this results in some non-dry practices as the internals to get_swagger_view are not extendable.

Ideally this application should work out of the box with swagger-js but this does not seem to be the case. At the very least there should be a documented path to get the two to work together.

patrickml commented 6 years ago

+1

bowbahdoe commented 6 years ago

+1

zbyte64 commented 6 years ago

Posting my solution, which is to implement a custom get_swagger_view that responds to application/json:

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

class JSONOpenAPIRender(renderers.OpenAPIRenderer):
    media_type = 'application/json'

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,
            JSONOpenAPIRender,
            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()
lduros commented 6 years ago

+1

lduros commented 6 years ago

Any update on this issue? It seems to be a bit of a dealbreaker if the main swagger javascript library doesn't work out of the box to fetch the schema from django-rest-swagger.

Thanks,

lduros commented 6 years ago

Simply adding the following as the schema_view in urls.py did the trick for me:

class JSONOpenAPIRenderer(renderers.OpenAPIRenderer):
    media_type = 'application/json'

schema_view = get_schema_view(title='My API',
                               renderer_classes=[JSONOpenAPIRenderer])
karthicksakkaravarti commented 5 years ago
from rest_framework_swagger import renderers
from rest_framework.decorators import api_view, renderer_classes
#function views
@api_view(['GET'])
@renderer_classes([renderers.OpenAPIRenderer, renderers.SwaggerUIRenderer,renderers.JSONRenderer])
def app_info(request):
    return Response({"hello":"hello"})