Skip to content
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

fix: collect routes from Host(#2118) #2183

Merged
merged 3 commits into from Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions starlette/schemas.py
Expand Up @@ -4,7 +4,7 @@

from starlette.requests import Request
from starlette.responses import Response
from starlette.routing import BaseRoute, Mount, Route
from starlette.routing import BaseRoute, Host, Mount, Route

try:
import yaml
Expand Down Expand Up @@ -49,9 +49,12 @@ def get_endpoints(
endpoints_info: list = []

for route in routes:
if isinstance(route, Mount):
path = self._remove_converter(route.path)
if isinstance(route, (Mount, Host)):
routes = route.routes or []
if isinstance(route, Mount):
path = self._remove_converter(route.path)
else:
path = ""
sub_endpoints = [
EndpointInfo(
path="".join((path, sub_endpoint.path)),
Expand Down
15 changes: 14 additions & 1 deletion tests/test_schemas.py
@@ -1,6 +1,6 @@
from starlette.applications import Starlette
from starlette.endpoints import HTTPEndpoint
from starlette.routing import Mount, Route, WebSocketRoute
from starlette.routing import Host, Mount, Route, Router, WebSocketRoute
from starlette.schemas import SchemaGenerator

schemas = SchemaGenerator(
Expand Down Expand Up @@ -123,6 +123,7 @@ def schema(request):
Route("/no-docstring", endpoint=no_docstring),
Route("/schema", endpoint=schema, methods=["GET"], include_in_schema=False),
Mount("/subapp", subapp),
Host("sub.domain.com", app=Router(routes=[Mount("/subapp2", subapp)])),
]
)

Expand Down Expand Up @@ -165,6 +166,13 @@ def test_schema_generation():
}
}
},
"/subapp2/subapp-endpoint": {
"get": {
"responses": {
200: {"description": "This endpoint is part of a subapp."}
}
}
},
"/users": {
"get": {
"responses": {
Expand Down Expand Up @@ -224,6 +232,11 @@ def test_schema_generation():
responses:
200:
description: This endpoint is part of a subapp.
/subapp2/subapp-endpoint:
get:
responses:
200:
description: This endpoint is part of a subapp.
/users:
get:
responses:
Expand Down