Open xennygrimmato opened 5 months ago
b2066229bd
)[!TIP] I'll email you at vstulsyan@gmail.com when I complete this pull request!
The sandbox appears to be unavailable or down.
I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.
fastapi/applications.py
✓ https://github.com/xennygrimmato/fastapi/commit/2b4088150457cec88d083a58069f7e624a540770 Edit
Modify fastapi/applications.py with contents:
• Add an optional parameter `router_class` to the `FastAPI` class constructor with a default value of `APIRouter`. This allows users to specify a custom router class when initializing a FastAPI application.
• Inside the `FastAPI` class constructor, check if `router_class` is not `None` and is a subclass of `APIRouter`. If so, instantiate the custom router class and assign it to `self.router`. Otherwise, instantiate the default `APIRouter` class.
• Ensure that the custom router instantiation correctly handles any dependencies, overrides, and custom parameters that might be required by the custom router class.
• Update the documentation strings within the `FastAPI` class to include information about the new `router_class` parameter and how it can be used to specify a custom router.
--- +++ @@ -64,7 +64,8 @@ def __init__( self: AppType, *, - debug: Annotated[ + router_class: Type[routing.APIRouter] = routing.APIRouter, + **extra: Annotated[ bool, Doc( """ @@ -82,6 +83,8 @@ """ **Note**: you probably shouldn't use this parameter, it is inherited from Starlette and supported for compatibility. + + * `router_class`: Custom router class to be used instead of the default `APIRouter`. --- @@ -857,7 +860,38 @@ This is not passed as a parameter to the `FastAPI` class to avoid giving the false idea that FastAPI would generate a different OpenAPI schema. It is only available as an attribute. - + if router_class and issubclass(router_class, routing.APIRouter): + self.router = router_class( + routes=routes, + redirect_slashes=redirect_slashes, + dependency_overrides_provider=self, + on_startup=on_startup, + on_shutdown=on_shutdown, + lifespan=lifespan, + default_response_class=default_response_class, + dependencies=dependencies, + callbacks=callbacks, + deprecated=deprecated, + include_in_schema=include_in_schema, + responses=responses, + generate_unique_id_function=generate_unique_id_function, + ) + else: + self.router = routing.APIRouter( + routes=routes, + redirect_slashes=redirect_slashes, + dependency_overrides_provider=self, + on_startup=on_startup, + on_shutdown=on_shutdown, + lifespan=lifespan, + default_response_class=default_response_class, + dependencies=dependencies, + callbacks=callbacks, + deprecated=deprecated, + include_in_schema=include_in_schema, + responses=responses, + generate_unique_id_function=generate_unique_id_function, + ) **Example** ```python
fastapi/applications.py
✓ Edit
Check fastapi/applications.py with contents:
Ran GitHub Actions for 2b4088150457cec88d083a58069f7e624a540770:
fastapi/routing.py
✓ https://github.com/xennygrimmato/fastapi/commit/b4e887117738e12830084c1a222f19d4e97b58d0 Edit
Modify fastapi/routing.py with contents:
• Modify the `APIRouter` class to add support for custom parameters in route decorators. This involves updating the decorator methods (e.g., `get`, `post`, `put`, etc.) to accept arbitrary keyword arguments.
• Store any custom parameters passed to the route decorators in the route's metadata. This will require modifying the internal representation of routes to include a field for custom metadata.
• Ensure that the custom parameters are accessible within the route handler, possibly by adding them to the request state or passing them as additional arguments to the handler function.
• Update the documentation strings within the `APIRouter` class to include information about the support for custom parameters in route decorators and how they can be used.
--- +++ @@ -1639,6 +1639,7 @@ [FastAPI docs about how to Generate Clients](https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function). """ ), + **custom_params: Any, ] = Default(generate_unique_id), ) -> Callable[[DecoratedCallable], DecoratedCallable]: """ @@ -1680,10 +1681,12 @@ response_model_exclude_none=response_model_exclude_none, include_in_schema=include_in_schema, response_class=response_class, + custom_params=custom_params, name=name, callbacks=callbacks, openapi_extra=openapi_extra, - generate_unique_id_function=generate_unique_id_function, + **custom_params: Any, + ] = Default(generate_unique_id), ) def put( @@ -1704,7 +1707,7 @@ Doc( """ The type to use for the response. - + custom_params=custom_params, It could be any valid Pydantic *field* type. So, it doesn't have to be a Pydantic model, it could be other things, like a `list`, `dict`, etc. @@ -2435,19 +2438,22 @@ responses=responses, deprecated=deprecated, methods=["POST"], - operation_id=operation_id, + **custom_params: Any, + ] = Default(generate_unique_id), response_model_include=response_model_include, response_model_exclude=response_model_exclude, response_model_by_alias=response_model_by_alias, response_model_exclude_unset=response_model_exclude_unset, response_model_exclude_defaults=response_model_exclude_defaults, + custom_params=custom_params, response_model_exclude_none=response_model_exclude_none, include_in_schema=include_in_schema, response_class=response_class, name=name, callbacks=callbacks, openapi_extra=openapi_extra, - generate_unique_id_function=generate_unique_id_function, + **custom_params: Any, + ] = Default(generate_unique_id), ) def delete( @@ -2468,7 +2474,7 @@ Doc( """ The type to use for the response. - + custom_params=custom_params, It could be any valid Pydantic *field* type. So, it doesn't have to be a Pydantic model, it could be other things, like a `list`, `dict`, etc. @@ -2780,6 +2786,7 @@ [FastAPI docs about how to Generate Clients](https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function). """ ), + **custom_params: Any, ] = Default(generate_unique_id), ) -> Callable[[DecoratedCallable], DecoratedCallable]: """ @@ -2818,13 +2825,15 @@ response_model_by_alias=response_model_by_alias, response_model_exclude_unset=response_model_exclude_unset, response_model_exclude_defaults=response_model_exclude_defaults, + custom_params=custom_params, response_model_exclude_none=response_model_exclude_none, include_in_schema=include_in_schema, response_class=response_class, name=name, callbacks=callbacks, openapi_extra=openapi_extra, - generate_unique_id_function=generate_unique_id_function, + **custom_params: Any, + ] = Default(generate_unique_id), ) def options( @@ -2845,7 +2854,7 @@ Doc( """ The type to use for the response. - + custom_params=custom_params, It could be any valid Pydantic *field* type. So, it doesn't have to be a Pydantic model, it could be other things, like a `list`, `dict`, etc. @@ -3157,6 +3166,7 @@ [FastAPI docs about how to Generate Clients](https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function). """ ), + **custom_params: Any, ] = Default(generate_unique_id), ) -> Callable[[DecoratedCallable], DecoratedCallable]: """ @@ -3195,13 +3205,15 @@ response_model_by_alias=response_model_by_alias, response_model_exclude_unset=response_model_exclude_unset, response_model_exclude_defaults=response_model_exclude_defaults, + custom_params=custom_params, response_model_exclude_none=response_model_exclude_none, include_in_schema=include_in_schema, response_class=response_class, name=name, callbacks=callbacks, openapi_extra=openapi_extra, - generate_unique_id_function=generate_unique_id_function, + **custom_params: Any, + ] = Default(generate_unique_id), ) def head( @@ -3222,7 +3234,7 @@ Doc( """ The type to use for the response. - + custom_params=custom_params, It could be any valid Pydantic *field* type. So, it doesn't have to be a Pydantic model, it could be other things, like a `list`, `dict`, etc.
fastapi/routing.py
✓ Edit
Check fastapi/routing.py with contents:
Ran GitHub Actions for b4e887117738e12830084c1a222f19d4e97b58d0:
docs/en/docs/tutorial/using-custom-routers.md
✓ https://github.com/xennygrimmato/fastapi/commit/0140952aa0143e3b89e090c487d1d88bcccb585d Edit
Create docs/en/docs/tutorial/using-custom-routers.md with contents:
• Create a new documentation page titled "Using Custom Routers" under the "Tutorial" section.
• In this documentation, explain how to define a custom router class by extending the `APIRouter` class and implementing any required custom logic.
• Provide an example of how to initialize a FastAPI application with a custom router by passing the custom router class to the `FastAPI` constructor.
• Include examples of how to use custom parameters in route decorators and how these parameters can be accessed within route handlers.
• Reference the modified files `fastapi/applications.py` and `fastapi/routing.py` to provide context and additional details.
docs/en/docs/tutorial/using-custom-routers.md
✓ Edit
Check docs/en/docs/tutorial/using-custom-routers.md with contents:
Ran GitHub Actions for 0140952aa0143e3b89e090c487d1d88bcccb585d:
I have finished reviewing the code for completeness. I did not find errors for sweep/make_it_easy_to_use_custom_apirouters
.
💡 To recreate the pull request edit the issue title or description. To tweak the pull request, leave a comment on the pull request.Something wrong? Let us know.
This is an automated message generated by Sweep AI.
Details
I'm facing a problem when using a custom router becomes a real hack. The current hacky solution:
As you can see, I have to patch app with a new router using
and then call
to be able to use it. Also, direct
call is required because router doesn't support custom params.
Checklist
- [X] Modify `fastapi/applications.py` ✓ https://github.com/xennygrimmato/fastapi/commit/2b4088150457cec88d083a58069f7e624a540770 [Edit](https://github.com/xennygrimmato/fastapi/edit/sweep/make_it_easy_to_use_custom_apirouters/fastapi/applications.py#L1-L10) - [X] Running GitHub Actions for `fastapi/applications.py` ✓ [Edit](https://github.com/xennygrimmato/fastapi/edit/sweep/make_it_easy_to_use_custom_apirouters/fastapi/applications.py#L1-L10) - [X] Modify `fastapi/routing.py` ✓ https://github.com/xennygrimmato/fastapi/commit/b4e887117738e12830084c1a222f19d4e97b58d0 [Edit](https://github.com/xennygrimmato/fastapi/edit/sweep/make_it_easy_to_use_custom_apirouters/fastapi/routing.py#L541-L571) - [X] Running GitHub Actions for `fastapi/routing.py` ✓ [Edit](https://github.com/xennygrimmato/fastapi/edit/sweep/make_it_easy_to_use_custom_apirouters/fastapi/routing.py#L541-L571) - [X] Create `docs/en/docs/tutorial/using-custom-routers.md` ✓ https://github.com/xennygrimmato/fastapi/commit/0140952aa0143e3b89e090c487d1d88bcccb585d [Edit](https://github.com/xennygrimmato/fastapi/edit/sweep/make_it_easy_to_use_custom_apirouters/docs/en/docs/tutorial/using-custom-routers.md) - [X] Running GitHub Actions for `docs/en/docs/tutorial/using-custom-routers.md` ✓ [Edit](https://github.com/xennygrimmato/fastapi/edit/sweep/make_it_easy_to_use_custom_apirouters/docs/en/docs/tutorial/using-custom-routers.md)