Open API now supplies a summary of operations

This commit is contained in:
Casper Weiss Bang 2020-05-08 21:38:40 +02:00
parent 56ff382b17
commit a12fe8f197

View File

@ -145,6 +145,7 @@ class AutoSchema(ViewInspector):
operation = {} operation = {}
operation['operationId'] = self.get_operation_id(path, method) operation['operationId'] = self.get_operation_id(path, method)
operation['summary'] = self.get_summary(path, method)
operation['description'] = self.get_description(path, method) operation['description'] = self.get_description(path, method)
parameters = [] parameters = []
@ -208,6 +209,9 @@ class AutoSchema(ViewInspector):
# with the 'title' method and join them together. # with the 'title' method and join them together.
return components[0] + ''.join(x.title() for x in components[1:]) return components[0] + ''.join(x.title() for x in components[1:])
def _camel_case_split(self, camel_case_str):
return " ".join(re.findall(r'[A-Z](?:[a-z]+|[A-Z]*(?=[A-Z]|$))', camel_case_str))
def get_operation_id_base(self, path, method, action): def get_operation_id_base(self, path, method, action):
""" """
Compute the base part for operation ID from the model, serializer or view name. Compute the base part for operation ID from the model, serializer or view name.
@ -245,6 +249,29 @@ class AutoSchema(ViewInspector):
return name return name
def get_summary(self, path, method):
"""
Compute an summary from the view type and get_operation_id_base method.
"""
if self.view.__doc__ is not None:
return self.view.__doc__.strip().split("\n")[0]
method_name = getattr(self.view, 'action', method.lower())
if is_list_view(path, method, self.view):
action = 'list'
elif method_name not in self.method_mapping:
action = method_name.replace("_", " ").title()
else:
action = self._camel_case_split(self.method_mapping[method.lower()])
name = self.get_operation_id_base(path, method, action)
if action == 'list':
action = 'List of'
return action + " " + name
def get_operation_id(self, path, method): def get_operation_id(self, path, method):
""" """
Compute an operation ID from the view type and get_operation_id_base method. Compute an operation ID from the view type and get_operation_id_base method.