Skip to content

Enhancement: Add root_path value to OpenAPI servers if present #2077

Open
@floxay

Description

@floxay

Summary

Currently the OpenAPI config servers URLs default to "/" even if a root_path is present, even though for example debug errors display the root_path. (ValueError on GET /api/ if a ValueError is raised in test_handler in the code below.)

It would be nice to have Litestar automatically add the root_path value --if present-- as the default instead.

Basic Example

Code:

from litestar import Litestar, get, Request


@get(path="/", sync_to_thread=False)
def test_handler(request: Request) -> dict[str, str | list]:
    return {
        "request.scope['root_path']": request.scope["root_path"],
        "app.openapi_config.servers": request.app.openapi_config.servers,
    }


app = Litestar(route_handlers=[test_handler], debug=True)

if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, port=5000, root_path="/api")

Current response (Litestar 2.0.0b4):

{
	"request.scope['root_path']": "/api",
	"app.openapi_config.servers": [
		{
			"url": "/",
			"description": null,
			"variables": null
		}
	]
}

Proposed:

{
	"request.scope['root_path']": "/api",
	"app.openapi_config.servers": [
		{
			"url": "/api",
			"description": null,
			"variables": null
		}
	]
}

Drawbacks and Impact

No response

Unresolved questions

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementThis is a new feature or requestGood First IssueThis is good for newcomers to take onHelp Wanted 🆘This is good for people to work onOpenAPIThis is related to our OpenAPI schema

    Type

    No type

    Projects

    Status

    Ideas

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions