From 49ddaaef49646f586ba6c5e583313e24a6fb3d83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Lecrenier?= Date: Thu, 12 Jan 2023 16:42:50 +0100 Subject: [PATCH] Fix missing_swap_indexes error code and handling of expires_at param... of create api key route --- meilisearch-types/src/error.rs | 7 ++++--- meilisearch-types/src/keys.rs | 19 +++++++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/meilisearch-types/src/error.rs b/meilisearch-types/src/error.rs index 0d7f126a5..614449ff8 100644 --- a/meilisearch-types/src/error.rs +++ b/meilisearch-types/src/error.rs @@ -15,7 +15,8 @@ use serde_cs::vec::CS; use crate::star_or::StarOr; use self::deserr_codes::{ - MissingApiKeyActions, MissingApiKeyExpiresAt, MissingApiKeyIndexes, MissingIndexUid, InvalidSwapIndexes, MissingSwapIndexesIndexes, + InvalidSwapIndexes, MissingApiKeyActions, MissingApiKeyExpiresAt, MissingApiKeyIndexes, + MissingIndexUid, MissingSwapIndexes, }; #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] @@ -280,7 +281,7 @@ MissingDocumentId , invalid , BAD_REQUEST ; MissingIndexUid , invalid , BAD_REQUEST ; MissingMasterKey , authentication, UNAUTHORIZED ; MissingPayload , invalid , BAD_REQUEST ; -MissingSwapIndexesIndexes , invalid , BAD_REQUEST ; +MissingSwapIndexes , invalid , BAD_REQUEST ; MissingTaskFilters , invalid , BAD_REQUEST ; NoSpaceLeftOnDevice , system , UNPROCESSABLE_ENTITY; PayloadTooLarge , invalid , PAYLOAD_TOO_LARGE ; @@ -516,7 +517,7 @@ impl DeserrJsonError { deserr::ErrorKind::MissingField { field }, location, )); - Self { msg: x.msg, code: MissingSwapIndexesIndexes.error_code(), _phantom: PhantomData } + Self { msg: x.msg, code: MissingSwapIndexes.error_code(), _phantom: PhantomData } } } diff --git a/meilisearch-types/src/keys.rs b/meilisearch-types/src/keys.rs index d736fd8c1..8543651c5 100644 --- a/meilisearch-types/src/keys.rs +++ b/meilisearch-types/src/keys.rs @@ -48,7 +48,7 @@ pub struct CreateApiKey { pub actions: Vec, #[deserr(error = DeserrJsonError, missing_field_error = DeserrJsonError::missing_api_key_indexes)] pub indexes: Vec>, - #[deserr(error = DeserrJsonError, from(&String) = parse_expiration_date -> TakeErrorMessage, missing_field_error = DeserrJsonError::missing_api_key_expires_at)] + #[deserr(error = DeserrJsonError, from(Option) = parse_expiration_date -> TakeErrorMessage, missing_field_error = DeserrJsonError::missing_api_key_expires_at)] pub expires_at: Option, } impl CreateApiKey { @@ -159,36 +159,39 @@ impl Display for ParseOffsetDateTimeError { impl std::error::Error for ParseOffsetDateTimeError {} fn parse_expiration_date( - string: &str, + string: Option, ) -> std::result::Result, TakeErrorMessage> { - let datetime = if let Ok(datetime) = OffsetDateTime::parse(string, &Rfc3339) { + let Some(string) = string else { + return Ok(None) + }; + let datetime = if let Ok(datetime) = OffsetDateTime::parse(&string, &Rfc3339) { datetime } else if let Ok(primitive_datetime) = PrimitiveDateTime::parse( - string, + &string, format_description!( "[year repr:full base:calendar]-[month repr:numerical]-[day]T[hour]:[minute]:[second]" ), ) { primitive_datetime.assume_utc() } else if let Ok(primitive_datetime) = PrimitiveDateTime::parse( - string, + &string, format_description!( "[year repr:full base:calendar]-[month repr:numerical]-[day] [hour]:[minute]:[second]" ), ) { primitive_datetime.assume_utc() } else if let Ok(date) = Date::parse( - string, + &string, format_description!("[year repr:full base:calendar]-[month repr:numerical]-[day]"), ) { PrimitiveDateTime::new(date, time!(00:00)).assume_utc() } else { - return Err(TakeErrorMessage(ParseOffsetDateTimeError(string.to_owned()))); + return Err(TakeErrorMessage(ParseOffsetDateTimeError(string))); }; if datetime > OffsetDateTime::now_utc() { Ok(Some(datetime)) } else { - Err(TakeErrorMessage(ParseOffsetDateTimeError(string.to_owned()))) + Err(TakeErrorMessage(ParseOffsetDateTimeError(string))) } }