File tree Expand file tree Collapse file tree 3 files changed +55
-2
lines changed Expand file tree Collapse file tree 3 files changed +55
-2
lines changed Original file line number Diff line number Diff line change @@ -91,8 +91,10 @@ def setup_swagger(app: web.Application,
91
91
with open (join (STATIC_PATH , "index.html" ), "r" ) as f :
92
92
app ["SWAGGER_TEMPLATE_CONTENT" ] = (
93
93
f .read ()
94
- .replace ("##SWAGGER_CONFIG##" , _swagger_def_url )
95
- .replace ("##STATIC_PATH##" , statics_path )
94
+ .replace ("##SWAGGER_CONFIG##" , '/{}{}' .
95
+ format (api_base_url .lstrip ('/' ), _swagger_def_url ))
96
+ .replace ("##STATIC_PATH##" , '/{}{}' .
97
+ format (api_base_url .lstrip ('/' ), statics_path ))
96
98
)
97
99
98
100
Original file line number Diff line number Diff line change @@ -166,4 +166,33 @@ Global Swagger YAML
166
166
167
167
setup_swagger(app, swagger_from_file = " example_swagger.yaml" ) # <-- Loaded Swagger from external YAML file
168
168
169
+ web.run_app(app, host = " 127.0.0.1" )
170
+
171
+ Nested applications
172
+ +++++++++++++++++++
173
+
174
+ :samp: `aiohttp-swagger ` is compatible with aiohttp `Nested applications <http://aiohttp.readthedocs.io/en/stable/web.html >`_ feature.
175
+ In this case `api_base_url ` argument of `setup_swagger ` function should be the same as `prefix ` argument of `add_subapp ` method:
176
+
177
+
178
+ .. code-block :: python
179
+
180
+ from aiohttp import web
181
+ from aiohttp_swagger import *
182
+
183
+ async def ping (request ):
184
+ return web.Response(text = " pong" )
185
+
186
+ sub_app = web.Application()
187
+
188
+ sub_app.router.add_route(' GET' , " /ping" , ping)
189
+
190
+ setup_swagger(sub_app,
191
+ swagger_from_file = " example_swagger.yaml" ,
192
+ api_base_url = ' /sub_app_prefix' )
193
+
194
+ app = web.Application()
195
+
196
+ app.add_subapp(prefix = ' /sub_app_prefix' , subapp = sub_app)
197
+
169
198
web.run_app(app, host = " 127.0.0.1" )
Original file line number Diff line number Diff line change @@ -272,3 +272,25 @@ def test_class_view(test_client, loop):
272
272
assert "/class_view" in result ['paths' ]
273
273
assert "patch" not in result ['paths' ]["/class_view" ]
274
274
275
+
276
+ @asyncio .coroutine
277
+ def test_sub_app (test_client , loop ):
278
+ sub_app = web .Application (loop = loop )
279
+ sub_app .router .add_route ('*' , "/class_view" , ClassView )
280
+ setup_swagger (sub_app , api_base_url = '/sub_app' )
281
+ app = web .Application (loop = loop )
282
+ app .add_subapp (prefix = '/sub_app' , subapp = sub_app )
283
+
284
+ client = yield from test_client (app )
285
+ # GET
286
+ resp = yield from client .get ('/sub_app/class_view' )
287
+ assert resp .status == 200
288
+ text = yield from resp .text ()
289
+ assert 'OK' in text
290
+ swagger_resp1 = yield from client .get ('/sub_app/api/doc/swagger.json' )
291
+ assert swagger_resp1 .status == 200
292
+ text = yield from swagger_resp1 .text ()
293
+ result = json .loads (text )
294
+ assert "/class_view" in result ['paths' ]
295
+ assert "get" in result ['paths' ]["/class_view" ]
296
+ assert "post" in result ['paths' ]["/class_view" ]
You can’t perform that action at this time.
0 commit comments