Browse Source

doc generation: use controller doc string

Bastien Sevajol 6 years ago
parent
commit
fcb4776bdd
3 changed files with 38 additions and 0 deletions
  1. 9 0
      hapic/decorator.py
  2. 6 0
      hapic/doc.py
  3. 23 0
      tests/func/test_doc.py

+ 9 - 0
hapic/decorator.py View File

@@ -45,6 +45,15 @@ class ControllerReference(object):
45 45
         self.wrapped = wrapped
46 46
         self.token = token
47 47
 
48
+    def get_doc_string(self) -> str:
49
+        if self.wrapper.__doc__:
50
+            return self.wrapper.__doc__.strip()
51
+
52
+        if self.wrapped.__doc__:
53
+            return self.wrapper.__doc__.strip()
54
+
55
+        return ''
56
+
48 57
 
49 58
 class ControllerWrapper(object):
50 59
     def before_wrapped_func(

+ 6 - 0
hapic/doc.py View File

@@ -199,6 +199,12 @@ class DocGenerator(object):
199 199
                 controller.description,
200 200
             )
201 201
 
202
+            # TODO BS 20171114: TMP code waiting refact of doc
203
+            doc_string = controller.reference.get_doc_string()
204
+            if doc_string:
205
+                for method in operations.keys():
206
+                    operations[method]['description'] = doc_string
207
+
202 208
             path = Path(path=swagger_path, operations=operations)
203 209
 
204 210
             if swagger_path in paths:

+ 23 - 0
tests/func/test_doc.py View File

@@ -124,3 +124,26 @@ class TestDocGeneration(Base):
124 124
                    'in': 'formData',
125 125
                    'type': 'file',
126 126
                } in doc['paths']['/upload']['post']['parameters']
127
+
128
+    def test_func__docstring__ok__simple_case(self):
129
+        hapic = Hapic()
130
+        hapic.set_context(MyContext())
131
+        app = bottle.Bottle()
132
+
133
+        # TODO BS 20171113: Make this test non-bottle
134
+        @hapic.with_api_doc()
135
+        def my_controller(hapic_data=None):
136
+            """
137
+            Hello doc
138
+            """
139
+            assert hapic_data
140
+            assert hapic_data.files
141
+
142
+        app.route('/upload', method='POST', callback=my_controller)
143
+        doc = hapic.generate_doc(app)
144
+
145
+        assert doc.get('paths')
146
+        assert '/upload' in doc['paths']
147
+        assert 'post' in doc['paths']['/upload']
148
+        assert 'description' in doc['paths']['/upload']['post']
149
+        assert 'Hello doc' == doc['paths']['/upload']['post']['description']