Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move our custom AutoSchema class to use DRF utilities for this #5433

Open
AlanCoding opened this issue Dec 3, 2019 · 0 comments
Open

Move our custom AutoSchema class to use DRF utilities for this #5433

AlanCoding opened this issue Dec 3, 2019 · 0 comments

Comments

@AlanCoding
Copy link
Member

ISSUE TYPE
  • Feature Idea
SUMMARY

In #5407 we are updating Django REST framework.

But in doing that, we are setting this to a old / deprecated kind of class

from rest_framework.schemas import SchemaGenerator, AutoSchema as DRFAuthSchema

    'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.AutoSchema',

We actually have our own AutoSchema class, which we conditionally inject into our views.

awx/awx/api/swagger.py

Lines 27 to 58 in e70c7ab

class AutoSchema(DRFAuthSchema):
def get_link(self, path, method, base_url):
link = super(AutoSchema, self).get_link(path, method, base_url)
try:
serializer = self.view.get_serializer()
except Exception:
serializer = None
warnings.warn('{}.get_serializer() raised an exception during '
'schema generation. Serializer fields will not be '
'generated for {} {}.'
.format(self.view.__class__.__name__, method, path))
link.__dict__['deprecated'] = getattr(self.view, 'deprecated', False)
# auto-generate a topic/tag for the serializer based on its model
if hasattr(self.view, 'swagger_topic'):
link.__dict__['topic'] = str(self.view.swagger_topic).title()
elif serializer and hasattr(serializer, 'Meta'):
link.__dict__['topic'] = str(
serializer.Meta.model._meta.verbose_name_plural
).title()
elif hasattr(self.view, 'model'):
link.__dict__['topic'] = str(self.view.model._meta.verbose_name_plural).title()
else:
warnings.warn('Could not determine a Swagger tag for path {}'.format(path))
return link
def get_description(self, path, method):
setattr(self.view.request, 'swagger_method', method)
description = super(AutoSchema, self).get_description(path, method)
return description

Compare...

DRF now wants to use rest_framework.schemas.openapi.AutoSchema by default:

https://www.django-rest-framework.org/api-guide/schemas/

https://github.com/encode/django-rest-framework/blob/7c3477dcdae558b40acfa116ed08eeb5700818eb/rest_framework/schemas/coreapi.py#L336

So our auto schema class is very specifically written for the old DRF schema class, and then takes it and does swagger stuff. There's a new class which already does swagger stuff. We should switch to that one, rewriting our custom class so that we have to maintain less of the logic ourselves.

Exactly how much we need to do is unclear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants