From 8bb260bf3efb876ea9f59ccdbd059ddd4c356ec4 Mon Sep 17 00:00:00 2001 From: ManyTheFish Date: Tue, 8 Nov 2022 14:37:34 +0100 Subject: [PATCH] Fix Index name parsing error message to fit the specification --- meilisearch-http/tests/auth/api_keys.rs | 4 +-- .../tests/documents/add_documents.rs | 2 +- .../tests/documents/update_documents.rs | 2 +- meilisearch-http/tests/index/create_index.rs | 2 +- .../tests/settings/get_settings.rs | 2 +- meilisearch-types/src/index_uid.rs | 6 ++-- meilisearch-types/src/keys.rs | 29 +++++++++++++++---- 7 files changed, 32 insertions(+), 15 deletions(-) diff --git a/meilisearch-http/tests/auth/api_keys.rs b/meilisearch-http/tests/auth/api_keys.rs index bcea51d3f..96f266f0c 100644 --- a/meilisearch-http/tests/auth/api_keys.rs +++ b/meilisearch-http/tests/auth/api_keys.rs @@ -352,7 +352,7 @@ async fn error_add_api_key_invalid_parameters_indexes() { assert_eq!(400, code, "{:?}", &response); let expected_response = json!({ - "message": r#"`{"name":"products"}` is not a valid index uid. It should be an array of string representing index names."#, + "message": r#"`indexes` field value `{"name":"products"}` is invalid. It should be an array of string representing index names."#, "code": "invalid_api_key_indexes", "type": "invalid_request", "link": "https://docs.meilisearch.com/errors#invalid_api_key_indexes" @@ -377,7 +377,7 @@ async fn error_add_api_key_invalid_index_uids() { let (response, code) = server.add_api_key(content).await; let expected_response = json!({ - "message": r#"`["invalid index # / \\name with spaces"]` is not a valid index uid. It should be an array of string representing index names."#, + "message": r#"`invalid index # / \name with spaces` is not a valid index uid. Index uid can be an integer or a string containing only alphanumeric characters, hyphens (-) and underscores (_)."#, "code": "invalid_api_key_indexes", "type": "invalid_request", "link": "https://docs.meilisearch.com/errors#invalid_api_key_indexes" diff --git a/meilisearch-http/tests/documents/add_documents.rs b/meilisearch-http/tests/documents/add_documents.rs index 8dd3ba39a..6f1fabeae 100644 --- a/meilisearch-http/tests/documents/add_documents.rs +++ b/meilisearch-http/tests/documents/add_documents.rs @@ -636,7 +636,7 @@ async fn error_document_add_create_index_bad_uid() { let (response, code) = index.add_documents(json!([{"id": 1}]), None).await; let expected_response = json!({ - "message": "invalid index uid `883 fj!`, the uid must be an integer or a string containing only alphanumeric characters a-z A-Z 0-9, hyphens - and underscores _.", + "message": "`883 fj!` is not a valid index uid. Index uid can be an integer or a string containing only alphanumeric characters, hyphens (-) and underscores (_).", "code": "invalid_index_uid", "type": "invalid_request", "link": "https://docs.meilisearch.com/errors#invalid_index_uid" diff --git a/meilisearch-http/tests/documents/update_documents.rs b/meilisearch-http/tests/documents/update_documents.rs index 1cc66a0c2..688605861 100644 --- a/meilisearch-http/tests/documents/update_documents.rs +++ b/meilisearch-http/tests/documents/update_documents.rs @@ -10,7 +10,7 @@ async fn error_document_update_create_index_bad_uid() { let (response, code) = index.update_documents(json!([{"id": 1}]), None).await; let expected_response = json!({ - "message": "invalid index uid `883 fj!`, the uid must be an integer or a string containing only alphanumeric characters a-z A-Z 0-9, hyphens - and underscores _.", + "message": "`883 fj!` is not a valid index uid. Index uid can be an integer or a string containing only alphanumeric characters, hyphens (-) and underscores (_).", "code": "invalid_index_uid", "type": "invalid_request", "link": "https://docs.meilisearch.com/errors#invalid_index_uid" diff --git a/meilisearch-http/tests/index/create_index.rs b/meilisearch-http/tests/index/create_index.rs index 0d4b01278..8e01a8113 100644 --- a/meilisearch-http/tests/index/create_index.rs +++ b/meilisearch-http/tests/index/create_index.rs @@ -189,7 +189,7 @@ async fn error_create_with_invalid_index_uid() { let (response, code) = index.create(None).await; let expected_response = json!({ - "message": "invalid index uid `test test#!`, the uid must be an integer or a string containing only alphanumeric characters a-z A-Z 0-9, hyphens - and underscores _.", + "message": "`test test#!` is not a valid index uid. Index uid can be an integer or a string containing only alphanumeric characters, hyphens (-) and underscores (_).", "code": "invalid_index_uid", "type": "invalid_request", "link": "https://docs.meilisearch.com/errors#invalid_index_uid" diff --git a/meilisearch-http/tests/settings/get_settings.rs b/meilisearch-http/tests/settings/get_settings.rs index fa45ad55e..a3c667047 100644 --- a/meilisearch-http/tests/settings/get_settings.rs +++ b/meilisearch-http/tests/settings/get_settings.rs @@ -182,7 +182,7 @@ async fn error_update_setting_unexisting_index_invalid_uid() { assert_eq!(code, 400); let expected = json!({ - "message": "invalid index uid `test##! `, the uid must be an integer or a string containing only alphanumeric characters a-z A-Z 0-9, hyphens - and underscores _.", + "message": "`test##! ` is not a valid index uid. Index uid can be an integer or a string containing only alphanumeric characters, hyphens (-) and underscores (_).", "code": "invalid_index_uid", "type": "invalid_request", "link": "https://docs.meilisearch.com/errors#invalid_index_uid"}); diff --git a/meilisearch-types/src/index_uid.rs b/meilisearch-types/src/index_uid.rs index 00e94c5b9..945a57e9e 100644 --- a/meilisearch-types/src/index_uid.rs +++ b/meilisearch-types/src/index_uid.rs @@ -75,9 +75,9 @@ impl fmt::Display for IndexUidFormatError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "invalid index uid `{}`, the uid must be an integer \ - or a string containing only alphanumeric characters \ - a-z A-Z 0-9, hyphens - and underscores _.", + "`{}` is not a valid index uid. Index uid can be an \ + integer or a string containing only alphanumeric \ + characters, hyphens (-) and underscores (_).", self.invalid_uid, ) } diff --git a/meilisearch-types/src/keys.rs b/meilisearch-types/src/keys.rs index cb0ec807e..2ec624809 100644 --- a/meilisearch-types/src/keys.rs +++ b/meilisearch-types/src/keys.rs @@ -1,4 +1,5 @@ use std::hash::Hash; +use std::str::FromStr; use enum_iterator::Sequence; use serde::{Deserialize, Serialize}; @@ -9,7 +10,7 @@ use time::{Date, OffsetDateTime, PrimitiveDateTime}; use uuid::Uuid; use crate::error::{Code, ErrorCode}; -use crate::index_uid::IndexUid; +use crate::index_uid::{IndexUid, IndexUidFormatError}; use crate::star_or::StarOr; type Result = std::result::Result; @@ -64,7 +65,15 @@ impl Key { let indexes = value .get("indexes") .map(|ind| { - from_value(ind.clone()).map_err(|_| Error::InvalidApiKeyIndexes(ind.clone())) + from_value::>(ind.clone()) + // If it's not a vec of string, return an API key parsing error. + .map_err(|_| Error::InvalidApiKeyIndexes(ind.clone())) + .and_then(|ind| { + ind.into_iter() + // If it's not a valid Index uid, return an Index Uid parsing error. + .map(|i| StarOr::::from_str(&i).map_err(Error::from)) + .collect() + }) }) .ok_or(Error::MissingParameter("indexes"))??; @@ -339,10 +348,10 @@ pub enum Error { MissingParameter(&'static str), #[error("`actions` field value `{0}` is invalid. It should be an array of string representing action names.")] InvalidApiKeyActions(Value), - #[error( - "`{0}` is not a valid index uid. It should be an array of string representing index names." - )] + #[error("`indexes` field value `{0}` is invalid. It should be an array of string representing index names.")] InvalidApiKeyIndexes(Value), + #[error("{0}")] + InvalidApiKeyIndexUid(IndexUidFormatError), #[error("`expiresAt` field value `{0}` is invalid. It should follow the RFC 3339 format to represents a date or datetime in the future or specified as a null value. e.g. 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MM:SS'.")] InvalidApiKeyExpiresAt(Value), #[error("`description` field value `{0}` is invalid. It should be a string or specified as a null value.")] @@ -357,12 +366,20 @@ pub enum Error { ImmutableField(String), } +impl From for Error { + fn from(e: IndexUidFormatError) -> Self { + Self::InvalidApiKeyIndexUid(e) + } +} + impl ErrorCode for Error { fn error_code(&self) -> Code { match self { Self::MissingParameter(_) => Code::MissingParameter, Self::InvalidApiKeyActions(_) => Code::InvalidApiKeyActions, - Self::InvalidApiKeyIndexes(_) => Code::InvalidApiKeyIndexes, + Self::InvalidApiKeyIndexes(_) | Self::InvalidApiKeyIndexUid(_) => { + Code::InvalidApiKeyIndexes + } Self::InvalidApiKeyExpiresAt(_) => Code::InvalidApiKeyExpiresAt, Self::InvalidApiKeyDescription(_) => Code::InvalidApiKeyDescription, Self::InvalidApiKeyName(_) => Code::InvalidApiKeyName,