From a4eb83e6a177c60a22480b2e1ccbc32ef61f2902 Mon Sep 17 00:00:00 2001 From: Mubelotix Date: Mon, 21 Jul 2025 15:27:33 +0200 Subject: [PATCH] Clean code --- crates/meilisearch-types/src/deserr/mod.rs | 3 -- crates/meilisearch-types/src/error.rs | 18 ----------- .../src/extractors/authentication/mod.rs | 9 +++--- .../meilisearch/src/routes/indexes/render.rs | 31 ++++++++++--------- 4 files changed, 20 insertions(+), 41 deletions(-) diff --git a/crates/meilisearch-types/src/deserr/mod.rs b/crates/meilisearch-types/src/deserr/mod.rs index ba1130c2e..f1470c201 100644 --- a/crates/meilisearch-types/src/deserr/mod.rs +++ b/crates/meilisearch-types/src/deserr/mod.rs @@ -213,6 +213,3 @@ merge_with_error_impl_take_error_message!(InvalidSearchSemanticRatio); merge_with_error_impl_take_error_message!(InvalidSearchRankingScoreThreshold); merge_with_error_impl_take_error_message!(InvalidSimilarRankingScoreThreshold); merge_with_error_impl_take_error_message!(InvalidSimilarId); -// merge_with_error_impl_take_error_message!(InvalidRenderTemplate); -// merge_with_error_impl_take_error_message!(InvalidRenderTemplateId); -// merge_with_error_impl_take_error_message!(InvalidRenderTemplateInline); diff --git a/crates/meilisearch-types/src/error.rs b/crates/meilisearch-types/src/error.rs index 8b3be1ede..caaedc384 100644 --- a/crates/meilisearch-types/src/error.rs +++ b/crates/meilisearch-types/src/error.rs @@ -657,24 +657,6 @@ impl fmt::Display for deserr_codes::InvalidNetworkSearchApiKey { } } -impl fmt::Display for deserr_codes::InvalidRenderTemplate { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "the value of `searchApiKey` is invalid, expected a string.") - } -} - -impl fmt::Display for deserr_codes::InvalidRenderTemplateId { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "the value of `searchApiKey` is invalid, expected a string.") - } -} - -impl fmt::Display for deserr_codes::InvalidRenderTemplateInline { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "the value of `searchApiKey` is invalid, expected a string.") - } -} - #[macro_export] macro_rules! internal_error { ($target:ty : $($other:path), *) => { diff --git a/crates/meilisearch/src/extractors/authentication/mod.rs b/crates/meilisearch/src/extractors/authentication/mod.rs index 0101b26d2..f9e38a68d 100644 --- a/crates/meilisearch/src/extractors/authentication/mod.rs +++ b/crates/meilisearch/src/extractors/authentication/mod.rs @@ -133,14 +133,12 @@ pub fn extract_token_from_request( } } -pub trait Policy { +pub trait Policy: Sized { fn authenticate( auth: Data, token: &str, index: Option<&str>, - ) -> Result - where - Self: Sized; + ) -> Result; } pub mod policies { @@ -352,7 +350,8 @@ pub mod policies { ) -> Result { let filter_a = ActionPolicy::::authenticate(auth.clone(), token, index)?; let _filter_b = ActionPolicy::::authenticate(auth, token, index)?; - + // There is no point merging the filters here. + // Since they originate from the same API key, they will hold the same information. Ok(filter_a) } } diff --git a/crates/meilisearch/src/routes/indexes/render.rs b/crates/meilisearch/src/routes/indexes/render.rs index ca841ba0e..0a763ad62 100644 --- a/crates/meilisearch/src/routes/indexes/render.rs +++ b/crates/meilisearch/src/routes/indexes/render.rs @@ -34,7 +34,7 @@ use crate::routes::indexes::render_analytics::RenderAggregator; #[openapi( paths(render_post), tags(( - name = "Render templates", + name = "Render documents", description = "The /render route allows rendering templates used by Meilisearch.", external_docs(url = "https://www.meilisearch.com/docs/reference/api/render"), )), @@ -45,34 +45,35 @@ pub fn configure(cfg: &mut web::ServiceConfig) { cfg.service(web::resource("").route(web::post().to(SeqHandler(render_post)))); } -/// Render templates with POST +/// Render documents with POST #[utoipa::path( post, path = "{indexUid}/render", - tag = "Render templates", - security(("Bearer" = ["settings.get", "settings.*", "*.get", "*"])), + tag = "Render documents", + security(("Bearer" = ["settings.get,documents.get", "*.get", "*"])), params(("indexUid" = String, Path, example = "movies", description = "Index Unique Identifier", nullable = false)), request_body = RenderQuery, responses( - (status = 200, description = "The rendered result is returned", body = RenderResult, content_type = "application/json", example = json!( + (status = 200, description = "The rendered result is returned along with the template", body = RenderResult, content_type = "application/json", example = json!( { + "template": "{{ doc.breed }} called {{ doc.name }}", "rendered": "A Jack Russell called Iko" } )), (status = 404, description = "Template or document not found", body = ResponseError, content_type = "application/json", example = json!( { - "message": "Index `movies` not found.", // TODO - "code": "index_not_found", + "message": "Document with ID `9999` not found.", + "code": "render_document_not_found", "type": "invalid_request", - "link": "https://docs.meilisearch.com/errors#index_not_found" + "link": "https://docs.meilisearch.com/errors#render_document_not_found" } )), - (status = 400, description = "Template couldn't be rendered", body = ResponseError, content_type = "application/json", example = json!( + (status = 400, description = "Parameters are incorrect", body = ResponseError, content_type = "application/json", example = json!( { - "message": "The Authorization header is missing. It must use the bearer authorization method.", // TODO - "code": "missing_authorization_header", - "type": "auth", - "link": "https://docs.meilisearch.com/errors#missing_authorization_header" + "message": "Indexing fragment `mistake` does not exist for embedder `rest`.\n Hint: Available indexing fragments are `basic`, `withBreed`.", + "code": "invalid_render_template_id", + "type": "invalid_request", + "link": "https://docs.meilisearch.com/errors#invalid_render_template_id" } )), ) @@ -91,7 +92,7 @@ pub async fn render_post( let index = index_scheduler.index(&index_uid)?; let query = params.into_inner(); - debug!(parameters = ?query, "Render template"); + debug!(parameters = ?query, "Render document"); let mut aggregate = RenderAggregator::from_query(&query); @@ -104,7 +105,7 @@ pub async fn render_post( let result = result?; - debug!(returns = ?result, "Render template"); + debug!(returns = ?result, "Render document"); Ok(HttpResponse::Ok().json(result)) }