From d9f5aadc520f22ad8011d0409b9df30cc867a5be Mon Sep 17 00:00:00 2001 From: Nguyen Khac Thanh Date: Tue, 13 Jun 2023 05:27:32 +0700 Subject: [PATCH 1/3] fix: collect routes from Host(2118) --- .gitignore | 1 + starlette/schemas.py | 14 +++++++++++++- tests/test_schemas.py | 15 ++++++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index bff8fa258..43ba53e7b 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ venv*/ .python-version build/ dist/ +env*/ diff --git a/starlette/schemas.py b/starlette/schemas.py index f939cb5a3..bf84f9af5 100644 --- a/starlette/schemas.py +++ b/starlette/schemas.py @@ -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 @@ -62,6 +62,18 @@ def get_endpoints( ] endpoints_info.extend(sub_endpoints) + elif isinstance(route, Host): + routes = route.routes or [] + sub_endpoints = [ + EndpointInfo( + path=sub_endpoint.path, + http_method=sub_endpoint.http_method, + func=sub_endpoint.func, + ) + for sub_endpoint in self.get_endpoints(routes) + ] + endpoints_info.extend(sub_endpoints) + elif not isinstance(route, Route) or not route.include_in_schema: continue diff --git a/tests/test_schemas.py b/tests/test_schemas.py index 26884b391..6c5b1d603 100644 --- a/tests/test_schemas.py +++ b/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( @@ -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)])), ] ) @@ -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": { @@ -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: From 1058489003e6ccda36c17487f4645fb1ce046b88 Mon Sep 17 00:00:00 2001 From: Nguyen Khac Thanh Date: Tue, 13 Jun 2023 05:50:36 +0700 Subject: [PATCH 2/3] Fixes encode/starlette#2118 --- starlette/schemas.py | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/starlette/schemas.py b/starlette/schemas.py index bf84f9af5..29ac79df3 100644 --- a/starlette/schemas.py +++ b/starlette/schemas.py @@ -49,9 +49,11 @@ def get_endpoints( endpoints_info: list = [] for route in routes: - if isinstance(route, Mount): - path = self._remove_converter(route.path) - routes = route.routes or [] + if isinstance(route, (Mount, Host)): + path = "" + if isinstance(route, Mount): + path = self._remove_converter(route.path) + routes = route.routes or [] # type: ignore sub_endpoints = [ EndpointInfo( path="".join((path, sub_endpoint.path)), @@ -62,18 +64,6 @@ def get_endpoints( ] endpoints_info.extend(sub_endpoints) - elif isinstance(route, Host): - routes = route.routes or [] - sub_endpoints = [ - EndpointInfo( - path=sub_endpoint.path, - http_method=sub_endpoint.http_method, - func=sub_endpoint.func, - ) - for sub_endpoint in self.get_endpoints(routes) - ] - endpoints_info.extend(sub_endpoints) - elif not isinstance(route, Route) or not route.include_in_schema: continue From 0b2dd6f31c0faa53c3e0207d26f6103256739428 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Tue, 13 Jun 2023 08:20:08 +0200 Subject: [PATCH 3/3] Remove mypy issue --- .gitignore | 1 - starlette/schemas.py | 5 +++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 43ba53e7b..bff8fa258 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,3 @@ venv*/ .python-version build/ dist/ -env*/ diff --git a/starlette/schemas.py b/starlette/schemas.py index 29ac79df3..72d93e7d7 100644 --- a/starlette/schemas.py +++ b/starlette/schemas.py @@ -50,10 +50,11 @@ def get_endpoints( for route in routes: if isinstance(route, (Mount, Host)): - path = "" + routes = route.routes or [] if isinstance(route, Mount): path = self._remove_converter(route.path) - routes = route.routes or [] # type: ignore + else: + path = "" sub_endpoints = [ EndpointInfo( path="".join((path, sub_endpoint.path)),