-
Notifications
You must be signed in to change notification settings - Fork 202
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
Axum bindings for utoipa - better path registration for axum #991
Comments
Just to clarify: The proposed solution doesn't just do away with the (redundant) paths, but also doesn't mention the (also ideally redundant) schema declaration. Does this proposal include automatic schema types derivation? |
Not yet at least, though getting away the redundant schema declarations would be also welcome improvement. However I am most likely not going to do it in same PR as this |
It seems to be relatively hard to create So far, unless this is solved someway or the other this issue will stay blocked. |
This PR adds a new crate `utiopa-axum` which provides bindings between `axum` and `utoipa`. It aims to blend as much as possible to the existing philosophy of axum way of registering handlers. This commit introduces new OpenApiRouter what wraps OpenApi and axum Router which provides passthrough implementation for most of the axum Router methods and collects and combines the OpenApi from registered routes. Routes registred only via `routes!()` macro will get added to the OpenApi. Also this commit introduces `routes!()` macro which collects axum handlers annotated with `#[utoipa::path()]` attribute macro to single paths intance which is then provided to the OpenApiRouter. Fixes #991
This PR adds a new crate `utiopa-axum` which provides bindings between `axum` and `utoipa`. It aims to blend as much as possible to the existing philosophy of axum way of registering handlers. This commit introduces new OpenApiRouter what wraps OpenApi and axum Router which provides passthrough implementation for most of the axum Router methods and collects and combines the OpenApi from registered routes. Routes registred only via `routes!()` macro will get added to the OpenApi. Also this commit introduces `routes!()` macro which collects axum handlers annotated with `#[utoipa::path()]` attribute macro to single paths intance which is then provided to the OpenApiRouter. Fixes #991
This PR adds a new crate `utoipa-axum` which provides bindings between `axum` and `utoipa`. It aims to blend as much as possible to the existing philosophy of axum way of registering handlers. This commit introduces new `OpenApiRouter` what wraps `OpenApi` and axum `Router` which provides passthrough implementation for most of the axum Router methods and collects and combines the `OpenApi` from registered routes. Routes registred only via `routes!()` macro will get added to the `OpenApi`. Also this commit introduces `routes!()` macro which collects axum handlers annotated with `#[utoipa::path()]` attribute macro to single paths intance which is then provided to the `OpenApiRouter`. Fixes #991
This PR adds a new crate `utoipa-axum` which provides bindings between `axum` and `utoipa`. It aims to blend as much as possible to the existing philosophy of axum way of registering handlers. This commit introduces new `OpenApiRouter` what wraps `OpenApi` and axum `Router` which provides passthrough implementation for most of the axum Router methods and collects and combines the `OpenApi` from registered routes. Routes registred only via `routes!()` macro will get added to the `OpenApi`. Also this commit introduces `routes!()` macro which collects axum handlers annotated with `#[utoipa::path()]` attribute macro to single paths intance which is then provided to the `OpenApiRouter`. Fixes #991
This PR adds a new crate `utoipa-axum` which provides bindings between `axum` and `utoipa`. It aims to blend as much as possible to the existing philosophy of axum way of registering handlers. This commit introduces new `OpenApiRouter` what wraps `OpenApi` and axum `Router` which provides passthrough implementation for most of the axum Router methods and collects and combines the `OpenApi` from registered routes. Routes registred only via `routes!()` macro will get added to the `OpenApi`. Also this commit introduces `routes!()` macro which collects axum handlers annotated with `#[utoipa::path()]` attribute macro to single paths intance which is then provided to the `OpenApiRouter`. Fixes #991
This PR adds a new crate `utoipa-axum` which provides bindings between `axum` and `utoipa`. It aims to blend as much as possible to the existing philosophy of axum way of registering handlers. This commit introduces new `OpenApiRouter` what wraps `OpenApi` and axum `Router` which provides passthrough implementation for most of the axum Router methods and collects and combines the `OpenApi` from registered routes. Routes registred only via `routes!()` macro will get added to the `OpenApi`. Also this commit introduces `routes!()` macro which collects axum handlers annotated with `#[utoipa::path()]` attribute macro to single paths intance which is then provided to the `OpenApiRouter`. Example of supported sytanx. ```rust let user_router: OpenApiRouter = OpenApiRouter::new() .routes(routes!(search_user)) .routes(routes!(get_user, post_user, delete_user)); ``` Fixes #991
Now there is a working implementation for axum's utoipa bindings leveraging macros when registering handlers for the router. Now the code above can be written like this with the implementation in PR #1004 #[derive(OpenApi)]
#[openapi(
tags(
(name = "todo", description = "Todo items management API")
)
)]
struct ApiDoc;
let mut todo_router = todo::router();
let todo_api = todo_router.to_openapi();
let api = ApiDoc::openapi().nest("/api/v1/todos", todo_api);
let app = Router::new()
.nest("/api/v1/todos", todo_router.into());
mod todo {
[derive(OpenApi)]
#[openapi(
components(schemas(Todo, TodoError))
)]
pub(super) struct TodoApi;
pub(super) fn router() -> OpenApiRouter {
let store = Arc::new(Store::default());
OpenApiRouter::with_openapi(TodoApi::openapi())
.routes(routes!(list_todos, create_todo, delete_todo, mark_done))
.routes(routes!(search_todo))
.with_state(store)
}
} Or even better? We can get rid of the duplicate nest call by only using the OpenApiRouter. let app = OpenApiRouter::new()
.nest("/api/v1/todos", todo::router());
let api = app.to_openapi();
let app: axum::Router = app.into(); |
This PR adds a new crate `utoipa-axum` which provides bindings between `axum` and `utoipa`. It aims to blend as much as possible to the existing philosophy of axum way of registering handlers. This commit introduces new `OpenApiRouter` what wraps `OpenApi` and axum `Router` which provides passthrough implementation for most of the axum Router methods and collects and combines the `OpenApi` from registered routes. Routes registred only via `routes!()` macro will get added to the `OpenApi`. Also this commit introduces `routes!()` macro which collects axum handlers annotated with `#[utoipa::path()]` attribute macro to single paths intance which is then provided to the `OpenApiRouter`. Example of supported sytanx. ```rust let user_router: OpenApiRouter = OpenApiRouter::new() .routes(routes!(search_user)) .routes(routes!(get_user, post_user, delete_user)); ``` Fixes #991
This PR adds a new crate `utoipa-axum` which provides bindings between `axum` and `utoipa`. It aims to blend as much as possible to the existing philosophy of axum way of registering handlers. This commit introduces new `OpenApiRouter` what wraps `OpenApi` and axum `Router` which provides passthrough implementation for most of the axum Router methods and collects and combines the `OpenApi` from registered routes. Routes registred only via `routes!()` macro will get added to the `OpenApi`. Also this commit introduces `routes!()` macro which collects axum handlers annotated with `#[utoipa::path()]` attribute macro to single paths intance which is then provided to the `OpenApiRouter`. Example of supported sytanx. ```rust let user_router: OpenApiRouter = OpenApiRouter::new() .routes(routes!(search_user)) .routes(routes!(get_user, post_user, delete_user)); ``` Fixes #991
This PR adds a new crate `utoipa-axum` which provides bindings between `axum` and `utoipa`. It aims to blend as much as possible to the existing philosophy of axum way of registering handlers. This commit introduces new `OpenApiRouter` what wraps `OpenApi` and axum `Router` which provides passthrough implementation for most of the axum Router methods and collects and combines the `OpenApi` from registered routes. Routes registred only via `routes!()` macro will get added to the `OpenApi`. Also this commit introduces `routes!()` macro which collects axum handlers annotated with `#[utoipa::path()]` attribute macro to single paths intance which is then provided to the `OpenApiRouter`. Example of supported sytanx. ```rust let user_router: OpenApiRouter = OpenApiRouter::new() .routes(routes!(search_user)) .routes(routes!(get_user, post_user, delete_user)); ``` Fixes #991
This utility reduces some boilerplate, but it makes |
Hm could you elaborate a bit more about your concern. Like in my understanding, in order to achieve this kind of "deep" bindings between the two ( Nonetheless as I see it, there is no way around it. This indeed ties the two together and to reverse that I am not sure how it could be done that it still would be optionally pluggable to the Even if there was some sort of generic Router that can be used to register paths the same registration needed to happen in |
Description
Currently with axum one needs to do duplicate work when adding utoipa
#[utoipa::path]
macros to theOpenApi
and then registering those handlers foraxum::Router
.Proposed Solution
Solution currently under experimenting makes huge improvement to the current one by allowing users directly use custom
OpenApiRouter
that handles#[utoipa::path]
attribute macro path registration behind the scenes and registers the route handlers on the single go.This completely makes OpenApi derive macro redundant but can be used if needed together with the router. The benefit is that there will be no more duplicate work and handlers can be registered once.This completely makesOpenApi
derive macro redundant in terms of path registration but schemas still need to be registered the old way or directly to theOpenApi
type.Relates #624 Relates #662 Relates #537 Relates #394
The text was updated successfully, but these errors were encountered: