pythonopenapisanic

Python Sanic ext 22.9.0 - Open API static YAML file loading


I am using sanic/sanic-ext 22.9.0 , I need to load a static open api yaml or json file rather than autogenerate so that when I access it as <url>/docs, the static yaml file is loaded and UI display either Swagger or redoc version of UI with the API definition. Any suggestion on how to achieve this.


Solution

  • You have a few options.

    OPTION 1

    Load your custom OAS into Sanic, and server that.

    @app.before_server_start
    async def load_oas(app: Sanic):
        custom_oas_dict = load_spec_from_yaml()
        app.ext.openapi.raw(custom_oas_dict)
    

    OPTION 2

    Turn off OAS and roll your own solution, including swagger/redoc, etc

    app.config.OAS = False
    

    OPTION 3

    Serve your custom OAS, and overwrite the HTML to point to your custom file.

    app.config.OAS_PATH_TO_REDOC_HTML = "/path/to/custom/redoc.html"
    app.config.OAS_PATH_TO_SWAGGER_HTML = "/path/to/custom/swagger.html"
    
    app.static("/custom/oas.json", "/path/to/custom/oas.json")