diff --git a/meilisearch-types/src/star_or.rs b/meilisearch-types/src/star_or.rs index d6ef8fefd..cd26a1fb0 100644 --- a/meilisearch-types/src/star_or.rs +++ b/meilisearch-types/src/star_or.rs @@ -6,7 +6,6 @@ use std::str::FromStr; use deserr::{DeserializeError, Deserr, MergeWithError, ValueKind}; use serde::de::Visitor; use serde::{Deserialize, Deserializer, Serialize, Serializer}; -use utoipa::{IntoParams, ToSchema}; use crate::deserr::query_params::FromQueryParameter; diff --git a/meilisearch-types/src/tasks.rs b/meilisearch-types/src/tasks.rs index 9c324749f..dd07982dd 100644 --- a/meilisearch-types/src/tasks.rs +++ b/meilisearch-types/src/tasks.rs @@ -9,7 +9,7 @@ use milli::Object; use roaring::RoaringBitmap; use serde::{Deserialize, Serialize, Serializer}; use time::{Duration, OffsetDateTime}; -use utoipa::{IntoParams, ToSchema}; +use utoipa::ToSchema; use uuid::Uuid; use crate::error::ResponseError; diff --git a/meilisearch/src/main.rs b/meilisearch/src/main.rs index 1b4023f7c..b66bfc5b8 100644 --- a/meilisearch/src/main.rs +++ b/meilisearch/src/main.rs @@ -24,7 +24,6 @@ use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; use tracing::level_filters::LevelFilter; use tracing_subscriber::layer::SubscriberExt as _; use tracing_subscriber::Layer; -use utoipa::OpenApi; #[global_allocator] static ALLOC: MiMalloc = MiMalloc; diff --git a/meilisearch/src/routes/mod.rs b/meilisearch/src/routes/mod.rs index e4f89fc38..dd5a347d2 100644 --- a/meilisearch/src/routes/mod.rs +++ b/meilisearch/src/routes/mod.rs @@ -8,8 +8,16 @@ use actix_web::web::Data; use actix_web::{web, HttpRequest, HttpResponse}; use index_scheduler::IndexScheduler; use meilisearch_auth::AuthController; +use meilisearch_types::error::ErrorType; use meilisearch_types::error::{Code, ResponseError}; +use meilisearch_types::settings::Checked; +use meilisearch_types::settings::FacetingSettings; +use meilisearch_types::settings::MinWordSizeTyposSetting; +use meilisearch_types::settings::PaginationSettings; +use meilisearch_types::settings::TypoSettings; use meilisearch_types::settings::{Settings, Unchecked}; +use meilisearch_types::task_view::DetailsView; +use meilisearch_types::task_view::TaskView; use meilisearch_types::tasks::{Kind, Status, Task, TaskId}; use serde::{Deserialize, Serialize}; use time::OffsetDateTime; @@ -19,6 +27,9 @@ use utoipa::ToSchema; use utoipa_rapidoc::RapiDoc; use utoipa_scalar::{Scalar, Servable as ScalarServable}; +use self::open_api_utils::OpenApiAuth; +use self::tasks::AllTasks; + const PAGINATION_DEFAULT_LIMIT: usize = 20; mod api_key; @@ -35,7 +46,9 @@ pub mod tasks; #[derive(OpenApi)] #[openapi( - nest((path = "/tasks", api = tasks::TaskApi) ), + nest((path = "/tasks", api = tasks::TaskApi) ), + modifiers(&OpenApiAuth), + components(schemas(Code, ErrorType, AllTasks, TaskView, Status, DetailsView, ResponseError, Settings, Settings, TypoSettings, MinWordSizeTyposSetting, FacetingSettings, PaginationSettings, SummarizedTaskView, Kind)) )] pub struct MeilisearchApi; diff --git a/meilisearch/src/routes/open_api_utils.rs b/meilisearch/src/routes/open_api_utils.rs index 71690f52d..89a3ef76a 100644 --- a/meilisearch/src/routes/open_api_utils.rs +++ b/meilisearch/src/routes/open_api_utils.rs @@ -2,7 +2,7 @@ use serde::Serialize; use utoipa::openapi::security::{HttpAuthScheme, HttpBuilder, SecurityScheme}; #[derive(Debug, Serialize)] -pub(super) struct OpenApiAuth; +pub struct OpenApiAuth; impl utoipa::Modify for OpenApiAuth { fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) { diff --git a/meilisearch/src/routes/tasks.rs b/meilisearch/src/routes/tasks.rs index b5d30aaff..bb22d7810 100644 --- a/meilisearch/src/routes/tasks.rs +++ b/meilisearch/src/routes/tasks.rs @@ -5,15 +5,11 @@ use deserr::Deserr; use index_scheduler::{IndexScheduler, Query, TaskId}; use meilisearch_types::deserr::query_params::Param; use meilisearch_types::deserr::DeserrQueryParamError; -use meilisearch_types::error::{deserr_codes::*, Code, ErrorType}; +use meilisearch_types::error::deserr_codes::*; use meilisearch_types::error::{InvalidTaskDateError, ResponseError}; use meilisearch_types::index_uid::IndexUid; -use meilisearch_types::settings::{ - Checked, FacetingSettings, MinWordSizeTyposSetting, PaginationSettings, Settings, TypoSettings, - Unchecked, -}; use meilisearch_types::star_or::{OptionStarOr, OptionStarOrList}; -use meilisearch_types::task_view::{DetailsView, TaskView}; +use meilisearch_types::task_view::TaskView; use meilisearch_types::tasks::{Kind, KindWithContent, Status}; use serde::Serialize; use serde_json::json; @@ -23,7 +19,7 @@ use time::{Date, Duration, OffsetDateTime, Time}; use tokio::task; use utoipa::{IntoParams, OpenApi, ToSchema}; -use super::open_api_utils::OpenApiAuth; +// use super::open_api_utils::OpenApiAuth; use super::{get_task_id, is_dry_run, SummarizedTaskView}; use crate::analytics::Analytics; use crate::extractors::authentication::policies::*; @@ -33,6 +29,29 @@ use crate::Opt; const DEFAULT_LIMIT: u32 = 20; + +#[derive(Debug, Serialize)] +pub struct OpenApiAuth; + +impl utoipa::Modify for OpenApiAuth { + fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) { + if let Some(schema) = openapi.components.as_mut() { + schema.add_security_scheme( + "Bearer", + utoipa::openapi::security::SecurityScheme::Http( + utoipa::openapi::security::HttpBuilder::new() + .scheme(utoipa::openapi::security::HttpAuthScheme::Bearer) + .bearer_format("Uuidv4, string or JWT") + .description(Some( +"An API key is a token that you provide when making API calls. Include the token in a header parameter called `Authorization`. +Example: `Authorization: Bearer 8fece4405662dd830e4cb265e7e047aab2e79672a760a12712d2a263c9003509`")) + .build(), + ), + ); + } + } +} + #[derive(OpenApi)] #[openapi( paths(get_tasks, delete_tasks, cancel_tasks, get_task), @@ -42,8 +61,6 @@ const DEFAULT_LIMIT: u32 = 20; external_docs(url = "https://www.meilisearch.com/docs/reference/api/tasks"), )), - modifiers(&OpenApiAuth), - components(schemas(Code, ErrorType, AllTasks, TaskView, Status, DetailsView, ResponseError, Settings, Settings, TypoSettings, MinWordSizeTyposSetting, FacetingSettings, PaginationSettings, SummarizedTaskView, Kind)) )] pub struct TaskApi;