From 322d6b8cfec154b5ee022b90d8a3760e25e358f3 Mon Sep 17 00:00:00 2001 From: marin postma Date: Wed, 23 Jun 2021 15:25:56 +0200 Subject: [PATCH 1/2] fix serialization bug in settings --- meilisearch-http/src/routes/settings.rs | 27 ++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/meilisearch-http/src/routes/settings.rs b/meilisearch-http/src/routes/settings.rs index 10ce7292e..07e9a7563 100644 --- a/meilisearch-http/src/routes/settings.rs +++ b/meilisearch-http/src/routes/settings.rs @@ -7,7 +7,7 @@ use crate::{error::ResponseError, index::Unchecked}; #[macro_export] macro_rules! make_setting_route { - ($route:literal, $type:ty, $attr:ident) => { + ($route:literal, $type:ty, $attr:ident, $camelcase_attr:literal) => { mod $attr { use actix_web::{web, HttpResponse}; @@ -51,7 +51,9 @@ macro_rules! make_setting_route { index_uid: actix_web::web::Path, ) -> std::result::Result { let settings = data.settings(index_uid.into_inner()).await?; - Ok(HttpResponse::Ok().json(settings.$attr)) + let mut json = serde_json::json!(&settings); + let val = json[$camelcase_attr].take(); + Ok(HttpResponse::Ok().json(val)) } } }; @@ -60,43 +62,50 @@ macro_rules! make_setting_route { make_setting_route!( "/indexes/{index_uid}/settings/filterable-attributes", std::collections::HashSet, - filterable_attributes + filterable_attributes, + "filterableAttributes" ); make_setting_route!( "/indexes/{index_uid}/settings/displayed-attributes", Vec, - displayed_attributes + displayed_attributes, + "displayedAttributes" ); make_setting_route!( "/indexes/{index_uid}/settings/searchable-attributes", Vec, - searchable_attributes + searchable_attributes, + "searchableAttributes" ); make_setting_route!( "/indexes/{index_uid}/settings/stop-words", std::collections::BTreeSet, - stop_words + stop_words, + "stopWords" ); make_setting_route!( "/indexes/{index_uid}/settings/synonyms", std::collections::BTreeMap>, - synonyms + synonyms, + "synonyms" ); make_setting_route!( "/indexes/{index_uid}/settings/distinct-attribute", String, - distinct_attribute + distinct_attribute, + "distinctAttribute" ); make_setting_route!( "/indexes/{index_uid}/settings/ranking-rules", Vec, - ranking_rules + ranking_rules, + "rankingRules" ); macro_rules! create_services { From b9b4feada8ca0fb09049b015e76fe875f2a1c840 Mon Sep 17 00:00:00 2001 From: marin postma Date: Wed, 23 Jun 2021 16:21:32 +0200 Subject: [PATCH 2/2] add tests --- .../tests/settings/get_settings.rs | 42 +++++++++++++++++-- meilisearch-http/tests/stats/mod.rs | 7 +--- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/meilisearch-http/tests/settings/get_settings.rs b/meilisearch-http/tests/settings/get_settings.rs index 804167517..4941b5d2f 100644 --- a/meilisearch-http/tests/settings/get_settings.rs +++ b/meilisearch-http/tests/settings/get_settings.rs @@ -1,5 +1,21 @@ +use std::collections::HashMap; + +use once_cell::sync::Lazy; +use serde_json::{Value, json}; + use crate::common::Server; -use serde_json::json; + +static DEFAULT_SETTINGS_VALUES: Lazy> = Lazy::new(|| { + let mut map = HashMap::new(); + map.insert("displayed_attributes", json!(["*"])); + map.insert("searchable_attributes", json!(["*"])); + map.insert("filterable_attributes", json!([])); + map.insert("distinct_attribute", json!(Value::Null)); + map.insert("ranking_rules", json!(["words", "typo", "proximity", "attribute", "exactness"])); + map.insert("stop_words", json!([])); + map.insert("synonyms", json!({})); + map +}); #[actix_rt::test] async fn get_settings_unexisting_index() { @@ -142,6 +158,7 @@ macro_rules! test_setting_routes { $( mod $setting { use crate::common::Server; + use super::DEFAULT_SETTINGS_VALUES; #[actix_rt::test] async fn get_unexisting_index() { @@ -177,8 +194,25 @@ macro_rules! test_setting_routes { .chars() .map(|c| if c == '_' { '-' } else { c }) .collect::()); - let (_response, code) = server.service.delete(url).await; - assert_eq!(code, 404); + let (response, code) = server.service.delete(url).await; + assert_eq!(code, 404, "{}", response); + } + + #[actix_rt::test] + async fn get_default() { + let server = Server::new().await; + let index = server.index("test"); + let (response, code) = index.create(None).await; + assert_eq!(code, 200, "{}", response); + let url = format!("/indexes/test/settings/{}", + stringify!($setting) + .chars() + .map(|c| if c == '_' { '-' } else { c }) + .collect::()); + let (response, code) = server.service.get(url).await; + assert_eq!(code, 200, "{}", response); + let expected = DEFAULT_SETTINGS_VALUES.get(stringify!($setting)).unwrap(); + assert_eq!(expected, &response); } } )* @@ -189,6 +223,8 @@ test_setting_routes!( filterable_attributes, displayed_attributes, searchable_attributes, + distinct_attribute, stop_words, + ranking_rules, synonyms ); diff --git a/meilisearch-http/tests/stats/mod.rs b/meilisearch-http/tests/stats/mod.rs index a5d08b52b..aba860256 100644 --- a/meilisearch-http/tests/stats/mod.rs +++ b/meilisearch-http/tests/stats/mod.rs @@ -54,11 +54,8 @@ async fn stats() { assert_eq!(code, 202, "{}", response); assert_eq!(response["updateId"], 0); - let (response, code) = server.stats().await; - - assert_eq!(code, 200, "{}", response); - - index.wait_update_id(0).await; + let response = index.wait_update_id(0).await; + println!("response: {}", response); let (response, code) = server.stats().await;