barrzen_axum_openapi/lib.rs
1//! Barrzen Axum OpenAPI
2//!
3//! OpenAPI documentation for Axum applications.
4
5use axum::Router;
6
7#[cfg(feature = "openapi")]
8use utoipa::openapi::OpenApi;
9#[cfg(feature = "openapi")]
10use utoipa_swagger_ui::SwaggerUi;
11
12/// Mount OpenAPI routes onto a router
13///
14/// Adds:
15/// - GET /docs - Swagger UI
16/// - GET /openapi.json - OpenAPI specification
17///
18/// Use this in your application to expose documentation.
19#[cfg(feature = "openapi")]
20pub fn mount(router: Router<()>, doc: OpenApi) -> Router<()> {
21 router.merge(SwaggerUi::new("/docs").url("/openapi.json", doc))
22}
23
24/// No-op when openapi feature is disabled behavior depends on caller handling the feature flag
25/// usually caller won't have `OpenApi` struct if feature is disabled.
26///
27/// So this function is strictly for when feature is enabled.
28///
29/// If feature is disabled, the `doc` argument type `OpenApi` won't exist.
30/// So we can't really have a no-op version with the same signature easily
31/// without generic wrapper or conditional compilation at call site.
32///
33/// Recommending call site to use `#[cfg(feature = "openapi")]`.
34#[cfg(not(feature = "openapi"))]
35#[cfg(not(feature = "openapi"))]
36pub fn mount(_router: Router<()>, _doc: ()) -> Router<()> {
37 // This signature is just a placeholder and unlikely to be used directly
38 // because `doc` param would be difficult to provide.
39 _router
40}