litestar-org/litestar

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

Open

#2 077 ouverte le 27 juil. 2023

Voir sur GitHub
 (11 commentaires) (0 réactions) (0 assignés)Python (550 forks)user submission
EnhancementGood First IssueHelp Wanted :sos:OpenAPI

Métriques du dépôt

Stars
 (8 243 stars)
Métriques de merge PR
 (Merge moyen 2j 22h) (53 PRs mergées en 30 j)

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

Guide contributeur