Skip to content

fix: duplication in request URL path when configuring OpenAPI #3306 #4072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion litestar/_openapi/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,28 @@
self._openapi: OpenAPI | None = None
self._openapi_schema: dict[str, object] | None = None

def _normalize_default_server(self) -> None:
"""If the default server is used along with app.path, it changes the server URL."""
if self.app.path == "/":
return
for server in self.openapi_config.servers:
if server.url == "/":
server.url = self.app.path

Check warning on line 76 in litestar/_openapi/plugin.py

View check run for this annotation

Codecov / codecov/patch

litestar/_openapi/plugin.py#L76

Added line #L76 was not covered by tests

def _normalize_route_path(self, route: HTTPRoute) -> str:
"""If app.path is used together with Open API config servers, the unnecessary part of the URL is removed.

Args:
route: http route object

Returns:
path: normalized path
"""
path = route.path_format or "/"
if self.app.path != "/":
return path.replace(self.app.path, "") or "/"

Check warning on line 89 in litestar/_openapi/plugin.py

View check run for this annotation

Codecov / codecov/patch

litestar/_openapi/plugin.py#L89

Added line #L89 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can easily lead to bugs, if self.app.path is contained somewhere within path. If we are to do this, it should use .removeprefix.

Copy link
Author

@StickKing StickKing Mar 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@provinzkraut Maybe use return path[len(self.app.path):] or "/" to support Python 3.8. ?

return path

def _build_openapi(self) -> OpenAPI:
openapi_config = self.openapi_config

Expand All @@ -75,11 +97,12 @@

ExampleFactory.seed_random(openapi_config.random_seed)

self._normalize_default_server()
openapi = openapi_config.to_openapi_schema()
context = OpenAPIContext(openapi_config=openapi_config, plugins=self.app.plugins.openapi)
path_items: dict[str, PathItem] = {}
for route in self.included_routes.values():
path = route.path_format or "/"
path = self._normalize_route_path(route)
path_item = create_path_item_for_route(context, route)
if existing_path_item := path_items.get(path):
path_item = merge_path_item_operations(existing_path_item, path_item, for_path=path)
Expand Down
Loading