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

feat!: static handler configuration [WIP] #3900

Draft
wants to merge 73 commits into
base: v3.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
8da3ec6
refactor(routing)!: Refactor routes and route handlers (#3386)
provinzkraut Apr 21, 2024
e633892
feat!: Make route handlers functional decorators (#3436)
provinzkraut May 25, 2024
0d0ad68
refactor(routing)!: Refactor routes and route handlers (#3386)
provinzkraut Apr 21, 2024
05023f2
refactor!: Remove deprecated `StaticFileConfig` (#3357)
provinzkraut Apr 10, 2024
8c40e5f
refactor(routing)!: Refactor routes and route handlers (#3386)
provinzkraut Apr 21, 2024
c3230b5
feat!: Make route handlers functional decorators (#3436)
provinzkraut May 25, 2024
3d5e53f
docs!: update to v3 style (#3324)
JacobCoffee Apr 10, 2024
4527216
refactor(routing)!: Refactor routes and route handlers (#3386)
provinzkraut Apr 21, 2024
b9e943a
refactor!: Remove deprecated `StaticFileConfig` (#3357)
provinzkraut Apr 10, 2024
6323236
docs!: update to v3 style (#3324)
JacobCoffee Apr 10, 2024
008a563
docs!: update to v3 style (#3324)
JacobCoffee Apr 10, 2024
da027aa
refactor!: Remove deprecated `StaticFileConfig` (#3357)
provinzkraut Apr 10, 2024
57a7023
refactor(routing)!: Refactor routes and route handlers (#3386)
provinzkraut Apr 21, 2024
75e40d6
feat!: Make route handlers functional decorators (#3436)
provinzkraut May 25, 2024
7661583
refactor(routing)!: Refactor routes and route handlers (#3386)
provinzkraut Apr 21, 2024
d7c82b9
feat!: Make route handlers functional decorators (#3436)
provinzkraut May 25, 2024
867c75f
refactor(routing)!: Refactor routes and route handlers (#3386)
provinzkraut Apr 21, 2024
286d224
feat!: Make route handlers functional decorators (#3436)
provinzkraut May 25, 2024
310785c
make route handlers functional decorators
provinzkraut Apr 27, 2024
14cb25f
Remove fn property
provinzkraut Apr 27, 2024
ef7fb85
Remove sync callable handling from on_registration
provinzkraut Apr 27, 2024
10125d8
Remove WS signature model
provinzkraut Apr 28, 2024
7721f38
Rename parameter_model -> kwargs_model
provinzkraut Apr 28, 2024
e181562
Reduce handler public API
provinzkraut Apr 28, 2024
7252f35
Simplify create_response_handlers
provinzkraut Apr 28, 2024
b6f582f
Remove app param from RouteHandler.on_registration
provinzkraut Apr 28, 2024
e2ee195
some progress to make it work somewhat
provinzkraut May 30, 2024
05d7855
wip
provinzkraut Nov 28, 2024
a8ddcd9
fix options
provinzkraut Nov 28, 2024
ab023cd
fix WS handler
provinzkraut Nov 28, 2024
ad90ccc
add a comment
provinzkraut Nov 28, 2024
843d64f
fix conflict
provinzkraut Nov 30, 2024
7ab3e9c
fix test
provinzkraut Nov 30, 2024
1b36438
remove merge artifacts
provinzkraut Nov 30, 2024
c5ac53b
fix WS resolution test
provinzkraut Nov 30, 2024
c7d3186
fix path param test
provinzkraut Nov 30, 2024
da7d55d
fix route registration process
provinzkraut Nov 30, 2024
7701d94
add a warning
provinzkraut Nov 30, 2024
0bff351
deprecate resolve_type_encoders
provinzkraut Nov 30, 2024
5f0c8fe
some cleanup
provinzkraut Nov 30, 2024
f3eee2c
fix stream
provinzkraut Dec 12, 2024
112d9a5
remove owner
provinzkraut Dec 12, 2024
f8f2eef
remove registration logic from router
provinzkraut Dec 12, 2024
0378a73
deprecate resolve_type_decoders
provinzkraut Dec 12, 2024
48c5a7f
remove _app shim
provinzkraut Dec 13, 2024
7944c6b
remove resolve_guards
provinzkraut Dec 13, 2024
2348ab8
formatting
provinzkraut Dec 13, 2024
dd50bb5
remove resolve_layered_parameters
provinzkraut Dec 13, 2024
87f79ec
remove resolve_dependencies
provinzkraut Dec 13, 2024
a774a7c
remove resolve_middleware
provinzkraut Dec 13, 2024
698f17a
remove resolve_exception_handlers
provinzkraut Dec 13, 2024
b55b56a
cleanup
provinzkraut Dec 13, 2024
9dd4843
remove resolve_opts
provinzkraut Dec 13, 2024
36fb48d
remove resolve_signature_namespace
provinzkraut Dec 13, 2024
ec373ad
resolve some conflicts
provinzkraut Dec 13, 2024
189afd4
simplify DTO resolution
provinzkraut Dec 14, 2024
302bb0a
simplify resolve request/response class
provinzkraut Dec 14, 2024
147ba57
remove resolve_response_headers
provinzkraut Dec 14, 2024
7415778
remove resolve_response_cookies
provinzkraut Dec 14, 2024
7510c35
remove resolve_before_request
provinzkraut Dec 14, 2024
95b0e96
remove resolve_after_response
provinzkraut Dec 14, 2024
6e9e9f3
remove resolve_after_request
provinzkraut Dec 14, 2024
7ba738c
remove resolve_include_in_schema
provinzkraut Dec 14, 2024
12f8bfb
remove resolve_security
provinzkraut Dec 14, 2024
f354a7a
remove resolve_tags
provinzkraut Dec 14, 2024
d76d9a6
remove resolve_request_max_body_size
provinzkraut Dec 14, 2024
8d5111c
cleanup
provinzkraut Dec 14, 2024
f4f8dc6
remove resolve_websocket_class
provinzkraut Dec 14, 2024
b66cdb8
simplify some resolution
provinzkraut Dec 14, 2024
d1c4aba
remove ownership layers
provinzkraut Dec 14, 2024
22662c8
remove copying during controller collection
provinzkraut Dec 14, 2024
0af76fe
linting fixes
provinzkraut Dec 14, 2024
a233d05
typing fixes
provinzkraut Dec 15, 2024
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
16 changes: 13 additions & 3 deletions litestar/_asgi/asgi_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"_plain_routes",
"_registered_routes",
"_static_routes",
"_trie_initialized",
"app",
"root_route_map_node",
"route_handler_index",
Expand All @@ -69,6 +70,7 @@
self._mount_routes: dict[str, RouteTrieNode] = {}
self._plain_routes: set[str] = set()
self._registered_routes: set[HTTPRoute | WebSocketRoute | ASGIRoute] = set()
self._trie_initialized = False
self.app = app
self.root_route_map_node: RouteTrieNode = create_node()
self.route_handler_index: dict[str, RouteHandlerType] = {}
Expand All @@ -94,7 +96,7 @@
ScopeState.from_scope(scope).exception_handlers = self._app_exception_handlers
raise
else:
ScopeState.from_scope(scope).exception_handlers = route_handler.resolve_exception_handlers()
ScopeState.from_scope(scope).exception_handlers = route_handler.exception_handlers
scope["route_handler"] = route_handler
scope["path_template"] = path_template
await asgi_app(scope, receive, send)
Expand Down Expand Up @@ -145,8 +147,16 @@

This map is used in the asgi router to route requests.
"""
new_routes = [route for route in self.app.routes if route not in self._registered_routes]
for route in new_routes:
if self._trie_initialized:
self._mount_paths_regex = None
self._mount_routes = {}
self._plain_routes = set()
self._registered_routes = set()
self.root_route_map_node = create_node()
self.route_handler_index = {}
self.route_mapping = defaultdict(list)

Check warning on line 157 in litestar/_asgi/asgi_router.py

View check run for this annotation

Codecov / codecov/patch

litestar/_asgi/asgi_router.py#L151-L157

Added lines #L151 - L157 were not covered by tests

for route in self.app.routes:
add_route_to_trie(
app=self.app,
mount_routes=self._mount_routes,
Expand Down
2 changes: 1 addition & 1 deletion litestar/_asgi/routing_trie/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def build_route_middleware_stack(
from litestar.routes import HTTPRoute

asgi_handler: ASGIApp = route.handle # type: ignore[assignment]
handler_middleware = route_handler.resolve_middleware()
handler_middleware = route_handler.middleware
has_cached_route = isinstance(route, HTTPRoute) and any(r.cache for r in route.route_handlers)
has_middleware = (
app.csrf_config or app.compression_config or has_cached_route or app.allowed_hosts or handler_middleware
Expand Down
2 changes: 1 addition & 1 deletion litestar/_kwargs/extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ async def _extract_multipart(
stream=connection.stream(),
boundary=connection.content_type[-1].get("boundary", "").encode(),
multipart_form_part_limit=multipart_form_part_limit,
type_decoders=connection.route_handler.resolve_type_decoders(),
type_decoders=connection.route_handler.type_decoders,
)
else:
form_values = scope_state.form
Expand Down
8 changes: 4 additions & 4 deletions litestar/_layers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@
from litestar.types.composite_types import ResponseCookies, ResponseHeaders


def narrow_response_headers(headers: ResponseHeaders | None) -> Sequence[ResponseHeader] | None:
def narrow_response_headers(headers: ResponseHeaders | None) -> Sequence[ResponseHeader]:
"""Given :class:`.types.ResponseHeaders` as a :class:`typing.Mapping`, create a list of
:class:`.datastructures.response_header.ResponseHeader` from it, otherwise return ``headers`` unchanged
"""
return (
tuple(ResponseHeader(name=name, value=value) for name, value in headers.items())
if isinstance(headers, Mapping)
else headers
)
) or ()


def narrow_response_cookies(cookies: ResponseCookies | None) -> Sequence[Cookie] | None:
def narrow_response_cookies(cookies: ResponseCookies | None) -> Sequence[Cookie]:
"""Given :class:`.types.ResponseCookies` as a :class:`typing.Mapping`, create a list of
:class:`.datastructures.cookie.Cookie` from it, otherwise return ``cookies`` unchanged
"""
return (
tuple(Cookie(key=key, value=value) for key, value in cookies.items())
if isinstance(cookies, Mapping)
else cookies
)
) or ()
2 changes: 1 addition & 1 deletion litestar/_openapi/datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,6 @@ def add_operation_id(self, operation_id: str) -> None:
if operation_id in self.operation_ids:
raise ImproperlyConfiguredException(
"operation_ids must be unique, "
f"please ensure the value of 'operation_id' is either not set or unique for {operation_id}"
f"please ensure the value of 'operation_id' is either not set or unique for {operation_id!r}"
)
self.operation_ids.add(operation_id)
4 changes: 2 additions & 2 deletions litestar/_openapi/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ def __init__(
self.schema_creator = SchemaCreator.from_openapi_context(self.context, prefer_alias=True)
self.route_handler = route_handler
self.parameters = ParameterCollection(route_handler)
self.dependency_providers = route_handler.resolve_dependencies()
self.layered_parameters = route_handler.resolve_layered_parameters()
self.dependency_providers = route_handler.dependencies
self.layered_parameters = route_handler.parameter_field_definitions
self.path_parameters = path_parameters

def create_parameter(self, field_definition: FieldDefinition, parameter_name: str) -> Parameter:
Expand Down
6 changes: 3 additions & 3 deletions litestar/_openapi/path_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def create_path_item(self) -> PathItem:
A PathItem instance.
"""
for http_method, route_handler in self.route.route_handler_map.items():
if not route_handler.resolve_include_in_schema():
if not route_handler.include_in_schema:
continue

operation = self.create_operation_for_handler_method(route_handler, HttpMethod(http_method))
Expand Down Expand Up @@ -74,14 +74,14 @@ def create_operation_for_handler_method(

return route_handler.operation_class(
operation_id=operation_id,
tags=route_handler.resolve_tags() or None,
tags=sorted(route_handler.tags) if route_handler.tags else None,
summary=route_handler.summary or SEPARATORS_CLEANUP_PATTERN.sub("", route_handler.handler_name.title()),
description=self.create_description_for_handler(route_handler),
deprecated=route_handler.deprecated,
responses=responses,
request_body=request_body,
parameters=parameters or None, # type: ignore[arg-type]
security=route_handler.resolve_security() or None,
security=list(route_handler.security) if route_handler.security else None,
)

def create_operation_id(self, route_handler: HTTPRouteHandler, http_method: HttpMethod) -> str:
Expand Down
2 changes: 1 addition & 1 deletion litestar/_openapi/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def receive_route(self, route: BaseRoute) -> None:
if not isinstance(route, HTTPRoute):
return

if any(route_handler.resolve_include_in_schema() for route_handler in route.route_handler_map.values()):
if any(route_handler.include_in_schema for route_handler in route.route_handler_map.values()):
# Force recompute the schema if a new route is added
self._openapi = None
self.included_routes[route.path] = route
2 changes: 1 addition & 1 deletion litestar/_openapi/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def set_success_response_headers(self, response: OpenAPIResponse) -> None:
else:
schema_creator = SchemaCreator.from_openapi_context(self.context, generate_examples=False)

for response_header in self.route_handler.resolve_response_headers():
for response_header in self.route_handler.response_headers:
header = OpenAPIHeader()
for attribute_name, attribute_value in (
(k, v) for k, v in asdict(response_header).items() if v is not None
Expand Down
Loading
Loading