Catch APIException in doc generation by sigvef · Pull Request #5443 · encode/django-rest-framework (original) (raw)
The documentation generator calls view.get_serializer() in order to
inspect it for documentation generation. However, if get_serializer()
throws an APIException (e.g. PermissionDenied), it doesn't get caught at
the call site, but instead propagates up and aborts the entire view.
With the try/except in this commit, the documentation generator instead
gratiously ignores that particular view and moves on to the next one
instead. Practical concequences of this commit is that the docs no
longer break if any view's get_serializer(..) throws an APIException.
@@ -11,6 +11,7 @@ |
---|
from django.utils.translation import ugettext_lazy as _ |
from rest_framework import serializers |
from rest_framework import exceptions |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this import one line up to resolve lint errors on python 2.7
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I'm inclined to take this.
There's just the point about the warning
.
Good work. Thanks!
try: |
---|
serializer = view.get_serializer() |
except exceptions.APIException: |
serializer = None |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need a warning here. Something like
"""
{}.get_serializer() raised an exception during schema
generation.
Serializer fields will not be generated for {} {}.
""".format(view.cls.__name__, method, path)
(The formatting there is more for GitHub than the code.)
This way we'll have some kind of console feedback so users can address any issues.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, added a warning.
Updated, ready for review.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. I think this is a good improvement. No-one wants an error-page. 🙂
Thanks for the input!
@sigvef Could just run isort rest_framework/schemas/inspectors.py
for me and push again please? (We've got that lint error again.)
Thanks!
The documentation generator calls view.get_serializer() in order to inspect it for documentation generation. However, if get_serializer() throws an APIException (e.g. PermissionDenied), it doesn't get caught at the call site, but instead propagates up and aborts the entire view. With the try/except in this commit, the documentation generator instead gratiously ignores that particular view and moves on to the next one instead. Practical concequences of this commit is that the docs no longer break if any view's get_serializer(..) throws an APIException.
Cool, I didn't know about isort. Updated again!
Thanks for a fast review by the way!
carltongibson pushed a commit that referenced this pull request
Set version number for 3.7.0 release
Rename release notes section
Moved issue links to top for easier access. (Can move back later)
Strict JSON handling
Add release note for #5250
Add release notes for #5170
Add release notes for #5443
Add release notes for #5448
Add release notes for #5452
Add release not for #5342
Add release notes for 5454
Remove Django 1.8 & 1.9 from README and setup.py
- Release notes for merged 3.6.5 milestone tickets
Tickets migrated to 3.7.0 milestone.
Add release notes for #5469
Add release notes from AM 2ndOct
Add final changes to the release notes.
Add date and milestone link
Move issue links back to bottom.
Update translations from transifex
Begin releae anouncement
Add release note for #5482
3.7 release announcement & related docs.