From 7565668a3624b23bbba56e5411727509c8930841 Mon Sep 17 00:00:00 2001 From: Mopsan Date: Tue, 5 May 2020 17:00:52 +0300 Subject: [PATCH] Add summary field to Path Item Object --- rest_framework/schemas/inspectors.py | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/rest_framework/schemas/inspectors.py b/rest_framework/schemas/inspectors.py index 027472db1..43b6f8993 100644 --- a/rest_framework/schemas/inspectors.py +++ b/rest_framework/schemas/inspectors.py @@ -69,6 +69,36 @@ class ViewInspector: def view(self): self._view = None + def get_summary(self, path, method): + """ + Determine a path summary. + + This will be based on the method docstring if one exists. + """ + view = self.view + + method_name = getattr(view, 'action', method.lower()) + method_docstring = getattr(view, method_name, None).__doc__ + if method_docstring: + # An explicit docstring on the method or action. + return self._get_summary_section(view, method.lower(), formatting.dedent(smart_str(method_docstring))) + + def _get_summary_section(self, view, header, description): + lines = [line for line in description.splitlines()] + current_section = '' + sections = {'': ''} + + for line in lines: + if self.header_regex.match(line): + current_section, separator, lead = line.partition(':') + sections[current_section] = lead.strip() + else: + sections[current_section] += '\n' + line + + if 'summary' in sections: + return sections['summary'].strip() + return '' + def get_description(self, path, method): """ Determine a path description.