Skip to content

Include root_path when looking up assets #11

@bundabrg

Description

@bundabrg

In patched_get_swagger_ui_html the urls only reference self.app.docs_url. This works fine until the app itself is mounted under another app. When this occurs the mounted app gets the base path passed to it via root_path.

IE:

def create_app() -> FastAPI:
    main_app = FastAPI(...)
    
    app = FastAPI(...)
    StandaloneDocs(app)
    app.include_router(myroute1, prefix='myroute1')
    app.include_router(myroute2, prefix='myroute2')
    
    main_app,mount("/myapp1", app)
    return main_app

In the above case, the full docs url is probably /myapp1/docs, but it then will try to read its static assets from /docs.

To fix this, patched_get_swagger_ui_html should prefix the passed urls to get_swager_ui_html with the apps current root_path.

Something like:

            def patched_get_swagger_ui_html(*args, **kwargs):
                return get_swagger_ui_html(
                    *args,
                    **kwargs,
                    swagger_favicon_url=swagger_favicon_url,
                    swagger_css_url=f"{self.app.root_path}{self.app.docs_url}/swagger-ui.css",
                    swagger_js_url=f"{self.app.root_path}{self.app.docs_url}/swagger-ui-bundle.js",
                )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions