aiohttp-swagger
                                
                                 aiohttp-swagger copied to clipboard
                                
                                    aiohttp-swagger copied to clipboard
                            
                            
                            
                        Setup swagger UI within main app, but consider indexing sub app as well
This flow is terribly broken currently. It's partially affected by aio-libs/aiohttp#2656. But also it becomes messy in terms of setup sequence.
For now I won't suggest a long-term solution (till upstream issue fixed), but here's what I had to hack to get this working:
"""The app configuration module."""
import logging
from aiohttp.web import Application
from aiohttp_swagger import setup_swagger
from .views import APIView
_logger = logging.getLogger(__name__)
async def build_application():
    """Create and pre-populate an aiohttp.web.Application instance."""
    app = Application()
    _logger.info('App initialized.')
    api_app = Application()
    _logger.info('API App initialized.')
    api_app.router.add_route('POST', r'/ping-cad/', APIView)
    _logger.info('Routes configured.')
    # Achtung! Keep add_subapp after routes declaration and
    # before swagger setup. Otherwise things will get fragile.
    # Affected by https://github.com/aio-libs/aiohttp/issues/2656
    app.add_subapp('/api/v1', api_app)
    _logger.info('API App mounted within main App.')
    # Accepts also `swagger_url='/api/v1/doc'`, defaults to '/api/doc':
    # we set api_base_url='/' because setting up within main App,
    # but referring API App, which after .add_subapp() has routes prepended
    # with /api/v1
    # We have to mount it before configuring swagger to make it recognise API.
    setup_swagger(app, api_base_url='/', swagger_url='/api/doc')
    _logger.info('Swagger Web UI configured.')
    return app
Note api_base_url reset to /, setup_swagger called with app, add_subapp() done before swagger setup, routes done before add_subapp()
Why I needed to setup it on the main app?
It's just that I've got a middleware processing error responses and converting them into JSON. And I didn't want that middleware to affect queries to swagger app.