From 7f80b116bc3d2c8f6a3da0858cdac6b08f6d372f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Lecrenier?= Date: Thu, 12 Jan 2023 09:35:56 +0100 Subject: [PATCH 1/2] Add specific immutable_field error codes --- dump/src/reader/compat/v5_to_v6.rs | 4 ++-- meilisearch-types/src/error.rs | 14 +++++++++++++- meilisearch-types/src/keys.rs | 12 ++++++------ meilisearch/src/routes/indexes/mod.rs | 27 ++++++++++++++++++++++++--- meilisearch/tests/auth/api_keys.rs | 12 ++++++------ 5 files changed, 51 insertions(+), 18 deletions(-) diff --git a/dump/src/reader/compat/v5_to_v6.rs b/dump/src/reader/compat/v5_to_v6.rs index e9e1b659b..237381414 100644 --- a/dump/src/reader/compat/v5_to_v6.rs +++ b/dump/src/reader/compat/v5_to_v6.rs @@ -292,14 +292,14 @@ impl From for v6::ResponseError { "malformed_payload" => v6::Code::MalformedPayload, "missing_payload" => v6::Code::MissingPayload, "api_key_not_found" => v6::Code::ApiKeyNotFound, - "missing_parameter" => v6::Code::UnretrievableErrorCode, + "missing_parameter" => v6::Code::BadRequest, "invalid_api_key_actions" => v6::Code::InvalidApiKeyActions, "invalid_api_key_indexes" => v6::Code::InvalidApiKeyIndexes, "invalid_api_key_expires_at" => v6::Code::InvalidApiKeyExpiresAt, "invalid_api_key_description" => v6::Code::InvalidApiKeyDescription, "invalid_api_key_name" => v6::Code::InvalidApiKeyName, "invalid_api_key_uid" => v6::Code::InvalidApiKeyUid, - "immutable_field" => v6::Code::ImmutableField, + "immutable_field" => v6::Code::BadRequest, "api_key_already_exists" => v6::Code::ApiKeyAlreadyExists, other => { log::warn!("Unknown error code {}", other); diff --git a/meilisearch-types/src/error.rs b/meilisearch-types/src/error.rs index b4a8b786e..bc29f9e82 100644 --- a/meilisearch-types/src/error.rs +++ b/meilisearch-types/src/error.rs @@ -181,7 +181,19 @@ DumpAlreadyProcessing , invalid , CONFLICT; DumpNotFound , invalid , NOT_FOUND; DumpProcessFailed , internal , INTERNAL_SERVER_ERROR; DuplicateIndexFound , invalid , BAD_REQUEST; -ImmutableField , invalid , BAD_REQUEST; + +ImmutableApiKeyUid , invalid , BAD_REQUEST; +ImmutableApiKeyKey , invalid , BAD_REQUEST; +ImmutableApiKeyActions , invalid , BAD_REQUEST; +ImmutableApiKeyIndexes , invalid , BAD_REQUEST; +ImmutableApiKeyExpiresAt , invalid , BAD_REQUEST; +ImmutableApiKeyCreatedAt , invalid , BAD_REQUEST; +ImmutableApiKeyUpdatedAt , invalid , BAD_REQUEST; + +ImmutableIndexUid , invalid , BAD_REQUEST; +ImmutableIndexCreatedAt , invalid , BAD_REQUEST; +ImmutableIndexUpdatedAt , invalid , BAD_REQUEST; + IndexAlreadyExists , invalid , CONFLICT ; IndexCreationFailed , internal , INTERNAL_SERVER_ERROR; IndexNotFound , invalid , NOT_FOUND; diff --git a/meilisearch-types/src/keys.rs b/meilisearch-types/src/keys.rs index 00d1a33eb..b41bb06b6 100644 --- a/meilisearch-types/src/keys.rs +++ b/meilisearch-types/src/keys.rs @@ -80,12 +80,12 @@ fn deny_immutable_fields_api_key( )); error.code = match field { - "uid" => Code::ImmutableField, - "actions" => Code::ImmutableField, - "indexes" => Code::ImmutableField, - "expiresAt" => Code::ImmutableField, - "createdAt" => Code::ImmutableField, - "updatedAt" => Code::ImmutableField, + "uid" => Code::ImmutableApiKeyUid, + "actions" => Code::ImmutableApiKeyActions, + "indexes" => Code::ImmutableApiKeyIndexes, + "expiresAt" => Code::ImmutableApiKeyExpiresAt, + "createdAt" => Code::ImmutableApiKeyCreatedAt, + "updatedAt" => Code::ImmutableApiKeyUpdatedAt, _ => Code::BadRequest, }; error diff --git a/meilisearch/src/routes/indexes/mod.rs b/meilisearch/src/routes/indexes/mod.rs index 19a4dfa6c..a6d5ab309 100644 --- a/meilisearch/src/routes/indexes/mod.rs +++ b/meilisearch/src/routes/indexes/mod.rs @@ -1,9 +1,11 @@ +use std::convert::Infallible; + use actix_web::web::Data; use actix_web::{web, HttpRequest, HttpResponse}; -use deserr::DeserializeFromValue; +use deserr::{DeserializeError, DeserializeFromValue, ValuePointerRef}; use index_scheduler::IndexScheduler; use log::debug; -use meilisearch_types::error::deserr_codes::*; +use meilisearch_types::error::{deserr_codes::*, unwrap_any, Code}; use meilisearch_types::error::{DeserrError, ResponseError, TakeErrorMessage}; use meilisearch_types::index_uid::IndexUid; use meilisearch_types::milli::{self, FieldDistribution, Index}; @@ -140,8 +142,27 @@ pub async fn create_index( } } +fn deny_immutable_fields_index( + field: &str, + accepted: &[&str], + location: ValuePointerRef, +) -> DeserrError { + let mut error = unwrap_any(DeserrError::::error::( + None, + deserr::ErrorKind::UnknownKey { key: field, accepted }, + location, + )); + + error.code = match field { + "uid" => Code::ImmutableIndexUid, + "createdAt" => Code::ImmutableIndexCreatedAt, + "updatedAt" => Code::ImmutableIndexUpdatedAt, + _ => Code::BadRequest, + }; + error +} #[derive(DeserializeFromValue, Debug)] -#[deserr(error = DeserrError, rename_all = camelCase, deny_unknown_fields)] +#[deserr(error = DeserrError, rename_all = camelCase, deny_unknown_fields = deny_immutable_fields_index)] pub struct UpdateIndexRequest { #[deserr(error = DeserrError)] primary_key: Option, diff --git a/meilisearch/tests/auth/api_keys.rs b/meilisearch/tests/auth/api_keys.rs index 485d1b551..0a14107a8 100644 --- a/meilisearch/tests/auth/api_keys.rs +++ b/meilisearch/tests/auth/api_keys.rs @@ -1404,9 +1404,9 @@ async fn error_patch_api_key_indexes() { meili_snap::snapshot!(meili_snap::json_string!(response, { ".createdAt" => "[ignored]", ".updatedAt" => "[ignored]" }), @r###" { "message": "Json deserialize error: unknown field `indexes`, expected one of `description`, `name` at ``.", - "code": "immutable_field", + "code": "immutable_api_key_indexes", "type": "invalid_request", - "link": "https://docs.meilisearch.com/errors#immutable-field" + "link": "https://docs.meilisearch.com/errors#immutable-api-key-indexes" } "###); meili_snap::snapshot!(code, @"400 Bad Request"); @@ -1481,9 +1481,9 @@ async fn error_patch_api_key_actions() { meili_snap::snapshot!(meili_snap::json_string!(response, { ".createdAt" => "[ignored]", ".updatedAt" => "[ignored]" }), @r###" { "message": "Json deserialize error: unknown field `actions`, expected one of `description`, `name` at ``.", - "code": "immutable_field", + "code": "immutable_api_key_actions", "type": "invalid_request", - "link": "https://docs.meilisearch.com/errors#immutable-field" + "link": "https://docs.meilisearch.com/errors#immutable-api-key-actions" } "###); meili_snap::snapshot!(code, @"400 Bad Request"); @@ -1550,9 +1550,9 @@ async fn error_patch_api_key_expiration_date() { meili_snap::snapshot!(meili_snap::json_string!(response, { ".createdAt" => "[ignored]", ".updatedAt" => "[ignored]" }), @r###" { "message": "Json deserialize error: unknown field `expiresAt`, expected one of `description`, `name` at ``.", - "code": "immutable_field", + "code": "immutable_api_key_expires_at", "type": "invalid_request", - "link": "https://docs.meilisearch.com/errors#immutable-field" + "link": "https://docs.meilisearch.com/errors#immutable-api-key-expires-at" } "###); meili_snap::snapshot!(code, @"400 Bad Request"); From 21b8cd53b70f987f93031dfdcd9b5ce096a88903 Mon Sep 17 00:00:00 2001 From: Tamo Date: Thu, 12 Jan 2023 16:20:24 +0100 Subject: [PATCH 2/2] reformat --- meilisearch/src/routes/indexes/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/meilisearch/src/routes/indexes/mod.rs b/meilisearch/src/routes/indexes/mod.rs index a6d5ab309..7a3a97c1f 100644 --- a/meilisearch/src/routes/indexes/mod.rs +++ b/meilisearch/src/routes/indexes/mod.rs @@ -5,8 +5,8 @@ use actix_web::{web, HttpRequest, HttpResponse}; use deserr::{DeserializeError, DeserializeFromValue, ValuePointerRef}; use index_scheduler::IndexScheduler; use log::debug; -use meilisearch_types::error::{deserr_codes::*, unwrap_any, Code}; -use meilisearch_types::error::{DeserrError, ResponseError, TakeErrorMessage}; +use meilisearch_types::error::deserr_codes::*; +use meilisearch_types::error::{unwrap_any, Code, DeserrError, ResponseError, TakeErrorMessage}; use meilisearch_types::index_uid::IndexUid; use meilisearch_types::milli::{self, FieldDistribution, Index}; use meilisearch_types::tasks::KindWithContent;