Merge ~lloydwaltersj/maas:oapi-line-break-op-desc into maas:master

Proposed by Jack Lloyd-Walters
Status: Merged
Approved by: Jack Lloyd-Walters
Approved revision: 3b18ced2992ada2ea068ca3c84345b197ec6c6e4
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~lloydwaltersj/maas:oapi-line-break-op-desc
Merge into: maas:master
Diff against target: 128 lines (+60/-7)
2 files modified
src/maasserver/api/doc_oapi.py (+17/-6)
src/maasserver/api/tests/test_oapi.py (+43/-1)
Reviewer Review Type Date Requested Status
MAAS Lander Approve
Adam Collard (community) Approve
Review via email: mp+428727@code.launchpad.net

Commit message

Remove ungrammatical linebreak in operation description

To post a comment you must log in.
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b oapi-line-break-op-desc lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas-tester/364/consoleText
COMMIT: 05fd9ccfcdb9b0725509fafb755e91539b2d9c29

review: Needs Fixing
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b oapi-line-break-op-desc lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas-tester/365/consoleText
COMMIT: da502e5fd6f5a0e4d399ff41443161312e042dd9

review: Needs Fixing
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b oapi-line-break-op-desc lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: 602d019fa8e0c9413ab5212df706a3a0a78b5a4e

review: Approve
Revision history for this message
Christian Grabowski (cgrabowski) :
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b oapi-line-break-op-desc lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas-tester/367/consoleText
COMMIT: 2fc42091a1a65aa326173ac8f7cf66a3eaf36030

review: Needs Fixing
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b oapi-line-break-op-desc lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: 3b18ced2992ada2ea068ca3c84345b197ec6c6e4

review: Approve
Revision history for this message
Adam Collard (adam-collard) :
review: Approve
Revision history for this message
MAAS Lander (maas-lander) wrote :

LANDING
-b oapi-line-break-op-desc lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED BUILD
LOG: http://maas-ci.internal:8080/job/maas-tester/377/consoleText

Revision history for this message
Jack Lloyd-Walters (lloydwaltersj) wrote :

jenkins: !test

Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b oapi-line-break-op-desc lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: 3b18ced2992ada2ea068ca3c84345b197ec6c6e4

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/maasserver/api/doc_oapi.py b/src/maasserver/api/doc_oapi.py
2index 543c571..c579acd 100644
3--- a/src/maasserver/api/doc_oapi.py
4+++ b/src/maasserver/api/doc_oapi.py
5@@ -9,6 +9,7 @@ This definition follows the rules and limitations of the ReST documentation.
6
7 from inspect import getdoc
8 import json
9+import re
10 from textwrap import dedent
11
12 from django.http import HttpResponse
13@@ -135,13 +136,21 @@ def _new_path_item(params):
14 return path_item
15
16
17+def _prettify(doc):
18+ """Cleans up text by replacing newlines with spaces, so that sentences are
19+ not broken apart prematurely.
20+ Respects paragraphing by not replacing newlines that occur after periods.
21+ """
22+ return re.sub("(?<![.\n])[\n]+", " ", dedent(doc)).strip()
23+
24+
25 def _render_oapi_oper_item(http_method, op, doc, uri_params, function):
26 oper_id = op or support.OperationsResource.crudmap.get(http_method)
27 oper_obj = {
28 "operationId": f"{doc.name}_{oper_id}",
29 "tags": [doc.handler.api_doc_section_name],
30 "summary": f"{doc.name} {oper_id}",
31- "description": dedent(doc.doc).strip(),
32+ "description": _prettify(doc.doc),
33 "responses": {},
34 }
35 oper_docstring = _oapi_item_from_docstring(
36@@ -192,9 +201,9 @@ def _oapi_item_from_docstring(function, http_method, uri_params):
37 ap.parse(docstring)
38 ap_dict = ap.get_dict()
39 oper_obj["summary"] = ap_dict["description_title"].strip()
40- oper_obj["description"] = ap_dict["description"].strip()
41+ oper_obj["description"] = _prettify(ap_dict["description"])
42 for param in ap_dict["params"]:
43- description = param["description_stripped"]
44+ description = _prettify(param["description_stripped"])
45 name = param["name"].strip("}{")
46 required = param["options"]["required"].lower() == "true"
47 if param["name"][0] == "{":
48@@ -222,8 +231,10 @@ def _oapi_item_from_docstring(function, http_method, uri_params):
49
50 for (status, content) in _response_pair(ap_dict):
51 response = {
52- "description": content.get(
53- "description_stripped", status["description_stripped"]
54+ "description": _prettify(
55+ content.get(
56+ "description_stripped", status["description_stripped"]
57+ )
58 ),
59 }
60 match content.get("type", "").lower():
61@@ -254,7 +265,7 @@ def _oapi_item_from_docstring(function, http_method, uri_params):
62
63 status_code = status["name"]
64 if not status_code.isdigit():
65- status_code = status["description_stripped"]
66+ status_code = _prettify(status["description_stripped"])
67 oper_obj.setdefault("responses", {}).update(
68 {status_code: response},
69 )
70diff --git a/src/maasserver/api/tests/test_oapi.py b/src/maasserver/api/tests/test_oapi.py
71index 0e27da9..d22c2cc 100644
72--- a/src/maasserver/api/tests/test_oapi.py
73+++ b/src/maasserver/api/tests/test_oapi.py
74@@ -5,7 +5,12 @@ import json
75
76 import yaml
77
78-from maasserver.api.doc_oapi import _render_oapi_paths, endpoint, landing_page
79+from maasserver.api.doc_oapi import (
80+ _prettify,
81+ _render_oapi_paths,
82+ endpoint,
83+ landing_page,
84+)
85 from maasserver.models.config import Config
86 from maasserver.testing.factory import factory
87 from maasserver.testing.testcase import MAASServerTestCase
88@@ -105,3 +110,40 @@ class TestOAPISpec(MAASServerTestCase):
89 for k, v in _get_all_key_values(_render_oapi_paths()):
90 if k == "type":
91 self.assertIn(v, self.oapi_types)
92+
93+
94+class TestPrettify(MAASTestCase):
95+ maxDiff = None
96+
97+ def test_cleans_newlines(self):
98+ before = """\
99+Returns system details -- for example, LLDP and
100+
101+``lshw`` XML dumps.
102+
103+
104+Returns a ``{detail_type: xml, ...}`` map, where
105+
106+``detail_type`` is something like "lldp" or "lshw".
107+
108+
109+Note that this is returned as BSON and not JSON. This is for
110+
111+efficiency, but mainly because JSON can''t do binary content without
112+
113+applying additional encoding like base-64. The example output below is
114+
115+represented in ASCII using ``bsondump example.bson`` and is for
116+
117+demonstrative purposes."""
118+
119+ after = """\
120+Returns system details -- for example, LLDP and ``lshw`` XML dumps.
121+
122+
123+Returns a ``{detail_type: xml, ...}`` map, where ``detail_type`` is something like "lldp" or "lshw".
124+
125+
126+Note that this is returned as BSON and not JSON. This is for efficiency, but mainly because JSON can''t do binary content without applying additional encoding like base-64. The example output below is represented in ASCII using ``bsondump example.bson`` and is for demonstrative purposes."""
127+
128+ self.assertEqual(_prettify(before), after)

Subscribers

People subscribed via source and target branches