litestar-org/litestar

Enhancement: Add `root_path` value to OpenAPI servers if present

Aberta

#2.077 aberto em 27 de jul. de 2023

 (11 comentários) (0 reação) (0 responsável)Python (550 forks)user submission
EnhancementGood First IssueHelp Wanted :sos:OpenAPI

Métricas do repositório

Stars
 (8.243 estrelas)
Métricas de merge de PR
 (Mesclagem média 13h 27m) (26 fundiu PRs em 30d)

Description

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

Guia do colaborador