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 599 forks source link

Django Swagger shown HTTP Error 500 and not processed HTML code when accessing without logged in user #486

Closed civantos closed 8 years ago

civantos commented 8 years ago

django-rest-swagger==2.0.1 djangorestframework==3.4.0

-- urls.py --

router = DefaultRouter()
router.register(r'documents', DocumentViewSet)
router.register(r'bounces', BounceViewSet)
router.register(r'users', UserViewSet)

#DOC SWAGGER API
url(r'^documentacion/$', esquema_doc_api),

-- views.py --

@api_view()
@renderer_classes([SwaggerUIRenderer, OpenAPIRenderer])
def esquema_doc_api(request):
    generator = schemas.SchemaGenerator(title='TEST API')
    return response.Response(generator.get_schema(request=request))

-- development server petitions log -- [19/Jul/2016 09:36:00] "GET /documentacion/?format=openapi HTTP/1.1" 500 83970

Now if I launch my project and type http://ip:port/documentacion/ in my browser I get an HTTP error 500 and display raw HTML content inside my Swagger view. The problem disappears when I use a logged in user. Anyway when l use a logged in user I still can see and endpoint with the same name that my scheme creator function 'esquema_doc_api' and returns HTML when I fire the GET method on it. I'm not pretty sure if it's supposed to be the standard behaviour.

I'll attach some screenshot with the problem.

Regards,

Javier Civantos

swagger_not_logged swagger_logged

marcgibbons commented 8 years ago

@civantos Could you provide the rendered error message? (can do so from the network tab).

I am guessing that if this only happens when the request is not provided in the schema generator, then it is possibly related to: https://github.com/tomchristie/django-rest-framework/issues/4278

civantos commented 8 years ago
AttributeError at /documentacion/
'dict' object has no attribute 'url'
Request Method: GET
Request URL:    http://192.168.112.105:8000/documentacion/?format=openapi
Django Version: 1.9.6
Exception Type: AttributeError
Exception Value:    
'dict' object has no attribute 'url'
Exception Location: /home/civan/tuvika/startup/local/lib/python2.7/site-packages/openapi_codec/encode.py in generate_swagger_object, line 8
Python Executable:  /home/civan/tuvika/startup/bin/python
Python Version: 2.7.9
Python Path:    
['/home/civan/tuvika/startup/startup',
 '/home/civan/tuvika/startup/lib/python2.7',
 '/home/civan/tuvika/startup/lib/python2.7/plat-x86_64-linux-gnu',
 '/home/civan/tuvika/startup/lib/python2.7/lib-tk',
 '/home/civan/tuvika/startup/lib/python2.7/lib-old',
 '/home/civan/tuvika/startup/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/home/civan/tuvika/startup/local/lib/python2.7/site-packages',
 '/home/civan/tuvika/startup/lib/python2.7/site-packages']
Server time:    Mar, 19 Jul 2016 11:40:16 +0200

- Traceback

Environment:

Request Method: GET
Request URL: http://192.168.112.105:8000/documentacion/?format=openapi

Django Version: 1.9.6
Python Version: 2.7.9
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rest_framework',
 'rest_framework_swagger',
 'fsm_admin',
 'django_cleanup',
 'import_export',
 'tabbed_admin',
 'corsheaders',
 'proyectos',
 'collab']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'corsheaders.middleware.CorsMiddleware']

Traceback:

File "/home/civan/tuvika/startup/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  174.                     response = self.process_exception_by_middleware(e, request)

File "/home/civan/tuvika/startup/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  172.                     response = response.render()

File "/home/civan/tuvika/startup/local/lib/python2.7/site-packages/django/template/response.py" in render
  160.             self.content = self.rendered_content

File "/home/civan/tuvika/startup/local/lib/python2.7/site-packages/rest_framework/response.py" in rendered_content
  70.         ret = renderer.render(self.data, media_type, context)

File "/home/civan/tuvika/startup/local/lib/python2.7/site-packages/rest_framework_swagger/renderers.py" in render
  19.         data = json.loads(codec.dump(data))

File "/home/civan/tuvika/startup/local/lib/python2.7/site-packages/openapi_codec/__init__.py" in dump
  34.         data = generate_swagger_object(document)

File "/home/civan/tuvika/startup/local/lib/python2.7/site-packages/openapi_codec/encode.py" in generate_swagger_object
  8.     parsed_url = urlparse.urlparse(document.url)

Exception Type: AttributeError at /documentacion/
Exception Value: 'dict' object has no attribute 'url'
konradhalas commented 8 years ago

I have the same problem, my stack:

Django==1.9.7
djangorestframework==3.4.0
django-rest-swagger==2.0.1

as a workaround, I required user to log in, before access docs.

civantos commented 8 years ago
from django.contrib.auth.decorators import login_required

@login_required()
@api_view()
@renderer_classes([SwaggerUIRenderer, OpenAPIRenderer])
def esquema_doc_api(request):
    generator = schemas.SchemaGenerator(title='TEST API')
    return response.Response(generator.get_schema(request=request))

Workaround works for me too, but there is still a warning about future function deprecations that may be related to this issue shown in my development server that I'm going to paste here so You can check:

/home/civan/tuvika/startup/local/lib/python2.7/site-packages/rest_framework_swagger/renderers.py:37: RemovedInDjango110Warning: render() must be called with a dict, not a RequestContext. return template.render(context)

/usr/lib/python2.7/importlib/init.py:37: RemovedInDjango110Warning: django.core.context_processors is deprecated in favor of django.template.context_processors. import(name)

bschellekens commented 8 years ago

I also got this and found a better solution (I think). The workaround you suggest to force login does not help our case, so I went into some debugging. Solved it by adding a permission_classes decorator. Might help for you.

The code:

from rest_framework.decorators import api_view, renderer_classes, permission_classes
from rest_framework import response, schemas, permissions
from rest_framework_swagger.renderers import OpenAPIRenderer, SwaggerUIRenderer

@api_view()
@permission_classes([permissions.AllowAny])
@renderer_classes([OpenAPIRenderer, SwaggerUIRenderer])
def schema_view(request):
    etc. etc.
ewiger commented 8 years ago

Apparently the difference is how the schema gets generated. Openapi gets generated as whole specification for every resources in the router. The error is thrown by the render that tries to dump a scheme for the whole router. Instead it gets serialized object as data param.

Correct way is to put schema-generation view as a separated url rule

url(r'^swagger/', schema_view),
angvp commented 8 years ago

I went with the solution proposed by @bschellekens and that remove that issue, but still I'm having this one: object has no attribute 'request' .

tomchristie commented 8 years ago

object has no attribute 'request' .

That's addressed here https://github.com/tomchristie/django-rest-framework/pull/4383 - new version 3.4.4 is due tomorrow. (Fri 12th Aug, 2016)

angvp commented 8 years ago

Awesome @tomchristie thanks!

angvp commented 8 years ago

Yep, confirming this can be closed with DRF 3.4.4

tomchristie commented 8 years ago

👍 great news, thanks for the update!

karingula commented 8 years ago

Hey guys,

whatever I do, still I'm having the same problem that Javier Civantos has showed in his First screenshot. Below is my error. would appreciate any help/suggestions--

18/Aug/2016 20:57:33] "GET /swagger/ HTTP/1.1" 403 3924 Internal Server Error: /swagger/ Traceback (most recent call last): File "/Users/karingul/Virtualenvs/django_rest/lib/python2.7/site-packages/django/core/handlers/exception.py", line 39, in inner response = get_response(request) File "/Users/karingul/Virtualenvs/django_rest/lib/python2.7/site-packages/django/core/handlers/base.py", line 217, in _get_response response = self.process_exception_by_middleware(e, request) File "/Users/karingul/Virtualenvs/django_rest/lib/python2.7/site-packages/django/core/handlers/base.py", line 215, in _get_response response = response.render() File "/Users/karingul/Virtualenvs/django_rest/lib/python2.7/site-packages/django/template/response.py", line 109, in render self.content = self.rendered_content File "/Users/karingul/Virtualenvs/django_rest/lib/python2.7/site-packages/rest_framework/response.py", line 72, in rendered_content ret = renderer.render(self.data, accepted_media_type, context) File "/Users/karingul/Virtualenvs/django_rest/lib/python2.7/site-packages/rest_framework_swagger/renderers.py", line 16, in render data = self.get_openapi_specification(data) File "/Users/karingul/Virtualenvs/django_rest/lib/python2.7/site-packages/rest_framework_swagger/renderers.py", line 29, in get_openapi_specification return json.loads(codec.dump(data)) File "/Users/karingul/Virtualenvs/django_rest/lib/python2.7/site-packages/openapi_codec/init.py", line 34, in dump data = generate_swagger_object(document) File "/Users/karingul/Virtualenvs/django_rest/lib/python2.7/site-packages/openapi_codec/encode.py", line 8, in generate_swagger_object parsed_url = urlparse.urlparse(document.url) AttributeError: 'dict' object has no attribute 'url' [18/Aug/2016 20:57:33] "GET /swagger/?format=openapi HTTP/1.1" 500 84799

marcgibbons commented 8 years ago

@karingula Better exception handling is coming in 2.0.5 (#523). Hoping to release this weekend.