Skip to content

feat(openapi): use root_path for default server URL#4625

Open
ndpvt-web wants to merge 1 commit intolitestar-org:mainfrom
ndpvt-web:feat/openapi-root-path-servers
Open

feat(openapi): use root_path for default server URL#4625
ndpvt-web wants to merge 1 commit intolitestar-org:mainfrom
ndpvt-web:feat/openapi-root-path-servers

Conversation

@ndpvt-web
Copy link
Copy Markdown

Description

When no servers are explicitly configured in OpenAPIConfig, this change uses the ASGI root_path from the request scope as the default server URL. This makes the OpenAPI schema work correctly when the app is deployed behind a reverse proxy with a path prefix.

Previously, the default server URL was always "/" regardless of the root_path setting. Now, if root_path is present in the scope and no custom servers are configured, the schema endpoint dynamically sets the server URL to the root_path value.

This follows the approach suggested by @peterschutt in the issue discussion.

Closes #2077

Changes

  • Modified litestar/_openapi/plugin.py to check for root_path in the request scope when serving the OpenAPI schema
  • If no explicit servers are configured and root_path is present, uses it as the default server URL

AI Disclosure

This PR was created with AI assistance (Claude by Anthropic). The implementation follows the approach outlined by maintainers in the issue discussion.

Co-Authored-By: AI Assistant (Claude) ai-assistant@contributor-bot.dev

When no servers are explicitly configured in OpenAPIConfig, use the
ASGI root_path from the request scope as the default server URL.
This makes the OpenAPI schema work correctly when the app is deployed
behind a reverse proxy with a path prefix.

Closes litestar-org#2077

Co-Authored-By: AI Assistant (Claude) <ai-assistant@contributor-bot.dev>
Signed-off-by: ndpvt-web <ndpvt-web@users.noreply.github.com>
@ndpvt-web ndpvt-web requested review from a team as code owners March 10, 2026 05:56
@github-actions github-actions bot added area/openapi This PR involves changes to the OpenAPI schema area/private-api This PR involves changes to the privatized API size: small type/feat pr/external Triage Required 🏥 This requires triage labels Mar 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/openapi This PR involves changes to the OpenAPI schema area/private-api This PR involves changes to the privatized API pr/external size: small Triage Required 🏥 This requires triage type/feat

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enhancement: Add root_path value to OpenAPI servers if present

1 participant