Skip to content

Commit

Permalink
cleanup optional summary implementation #97
Browse files Browse the repository at this point in the history
  • Loading branch information
tfranzel committed Jun 17, 2020
1 parent 2a8c352 commit 5a1c14c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
12 changes: 8 additions & 4 deletions drf_spectacular/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ def get_operation(self, path, path_regex, method, registry: ComponentRegistry):
self.method = method

operation = {}

summary = self.get_summary()
if summary:
operation['summary'] = summary

operation['operationId'] = self.get_operation_id()
operation['description'] = self.get_description()

summary = self.get_summary()
if summary:
operation['summary'] = summary

parameters = self._get_parameters()
if parameters:
operation['parameters'] = parameters
Expand Down Expand Up @@ -175,6 +175,10 @@ def get_description(self):
action_doc = inspect.getdoc(action_or_method) or ''
return action_doc or view_doc

def get_summary(self):
""" override this for custom behaviour """
return None

def get_auth(self):
"""
Obtains authentication classes and permissions from view. If authentication
Expand Down
13 changes: 7 additions & 6 deletions drf_spectacular/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ def __init__(self, name, type=str, location=QUERY, required=False, description='


def extend_schema(
summary=None,
operation_id=None,
parameters=None,
request=None,
responses=None,
auth=None,
description=None,
summary=None,
deprecated=None,
tags=None,
exclude=False,
Expand All @@ -64,7 +64,6 @@ def extend_schema(
decorator for the "view" kind. partially or completely overrides what would be
generated by drf-spectacular.
:param summary: An optional, string summary, intended to apply to all operations in this path.
:param operation_id: replaces the auto-generated operation_id. make sure there
are no naming collisions.
:param parameters: list of additional or replacement parameters added to the
Expand All @@ -81,6 +80,7 @@ def extend_schema(
:param request: replaces the discovered ``Serializer``.
:param auth:
:param description: replaces discovered doc strings
:param summary: an optional short summary of the description
:param deprecated: mark operation as deprecated
:param tags: override default list of tags
:param exclude: set True to exclude operation from schema
Expand Down Expand Up @@ -124,10 +124,6 @@ def get_operation(self, path, path_regex, method, registry):
return operation
return super().get_operation(path, path_regex, method, registry)

def get_summary(self):
if summary and is_in_scope(self):
return summary

def get_operation_id(self):
if operation_id and is_in_scope(self):
return operation_id
Expand Down Expand Up @@ -158,6 +154,11 @@ def get_description(self):
return description
return super().get_description()

def get_summary(self):
if summary and is_in_scope(self):
return summary
return super().get_summary()

def is_deprecated(self):
if deprecated and is_in_scope(self):
return deprecated
Expand Down
1 change: 1 addition & 0 deletions tests/test_extend_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class DoesItAllViewset(viewsets.GenericViewSet):
)
],
description='this weird endpoint needs some explaining',
summary='short summary',
deprecated=True,
tags=['custom_tag'],
)
Expand Down
1 change: 1 addition & 0 deletions tests/test_extend_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ paths:
post:
operationId: customname_create
description: this weird endpoint needs some explaining
summary: short summary
parameters:
- in: query
name: expiration_date
Expand Down

0 comments on commit 5a1c14c

Please sign in to comment.