Skip to content

Commit

Permalink
docs: Fixes for default openapi docs (#271)
Browse files Browse the repository at this point in the history
- Add version
- Remove openapi docs from `_docs/*` routes
  • Loading branch information
spencewenski authored Jul 6, 2024
1 parent d304973 commit e3ce636
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
33 changes: 10 additions & 23 deletions src/api/http/docs.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
use crate::api::http::build_path;
use crate::app::context::AppContext;
use aide::axum::routing::get_with;
use aide::axum::{ApiRouter, IntoApiResponse};
use aide::openapi::OpenApi;
use aide::redoc::Redoc;
use aide::scalar::Scalar;
use axum::extract::FromRef;
use axum::response::IntoResponse;
use axum::routing::get;
use axum::{Extension, Json};
use std::ops::Deref;
use std::sync::Arc;

const TAG: &str = "Docs";

/// This API is only available when using Aide.
pub fn routes<S>(parent: &str, state: &S) -> ApiRouter<S>
where
Expand All @@ -27,36 +25,25 @@ where
return router;
}

let router = router.api_route(
&open_api_schema_path,
get_with(docs_get, |op| op.description("OpenAPI schema").tag(TAG)),
);
let router = router.route(&open_api_schema_path, get(docs_get));

let router = if scalar_enabled(&context) {
router.api_route_with(
router.route(
&build_path(parent, scalar_route(&context)),
get_with(
Scalar::new(&open_api_schema_path)
.with_title(&context.config().app.name)
.axum_handler(),
|op| op.description("Documentation page.").tag(TAG),
),
|p| p.security_requirement("ApiKey"),
get(Scalar::new(&open_api_schema_path)
.with_title(&context.config().app.name)
.axum_handler()),
)
} else {
router
};

let router = if redoc_enabled(&context) {
router.api_route_with(
router.route(
&build_path(parent, redoc_route(&context)),
get_with(
Redoc::new(&open_api_schema_path)
.with_title(&context.config().app.name)
.axum_handler(),
|op| op.description("Redoc documentation page.").tag(TAG),
),
|p| p.security_requirement("ApiKey"),
get(Redoc::new(&open_api_schema_path)
.with_title(&context.config().app.name)
.axum_handler()),
)
} else {
router
Expand Down
13 changes: 11 additions & 2 deletions src/service/http/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,24 @@ where
let router = Router::<S>::new();

#[cfg(feature = "open-api")]
let app_name = AppContext::from_ref(state).config().app.name.clone();
let context = AppContext::from_ref(state);

Self {
state: state.clone(),
router,
#[cfg(feature = "open-api")]
api_router: default_api_routes(path_root.unwrap_or_default(), state),
#[cfg(feature = "open-api")]
api_docs: Box::new(move |api| {
api.title(&app_name).description(&format!("# {}", app_name))
let api = api
.title(&context.config().app.name)
.description(&format!("# {}", context.config().app.name));
let api = if let Some(version) = context.metadata().version.as_ref() {
api.version(version)
} else {
api
};
api
}),
middleware: default_middleware(state),
initializers: default_initializers(state),
Expand Down

0 comments on commit e3ce636

Please sign in to comment.