Add support for seq values

This commit is contained in:
Pedro Paulo de Amorim 2020-03-31 17:56:35 +01:00
parent 006e54109b
commit 1b47a10e89
2 changed files with 76 additions and 6 deletions

View File

@ -8,7 +8,7 @@ pub struct ConvertToString;
impl ser::Serializer for ConvertToString { impl ser::Serializer for ConvertToString {
type Ok = String; type Ok = String;
type Error = SerializerError; type Error = SerializerError;
type SerializeSeq = ser::Impossible<Self::Ok, Self::Error>; type SerializeSeq = SeqConvertToString;
type SerializeTuple = ser::Impossible<Self::Ok, Self::Error>; type SerializeTuple = ser::Impossible<Self::Ok, Self::Error>;
type SerializeTupleStruct = ser::Impossible<Self::Ok, Self::Error>; type SerializeTupleStruct = ser::Impossible<Self::Ok, Self::Error>;
type SerializeTupleVariant = ser::Impossible<Self::Ok, Self::Error>; type SerializeTupleVariant = ser::Impossible<Self::Ok, Self::Error>;
@ -135,8 +135,8 @@ impl ser::Serializer for ConvertToString {
} }
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
Err(SerializerError::UnserializableType { Ok(SeqConvertToString {
type_name: "sequence", text: String::new(),
}) })
} }
@ -254,3 +254,26 @@ impl ser::SerializeStruct for StructConvertToString {
Ok(self.text) Ok(self.text)
} }
} }
pub struct SeqConvertToString {
text: String,
}
impl ser::SerializeSeq for SeqConvertToString {
type Ok = String;
type Error = SerializerError;
fn serialize_element<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
where
T: ser::Serialize,
{
let text = key.serialize(ConvertToString)?;
self.text.push_str(&text);
self.text.push_str(" ");
Ok(())
}
fn end(self) -> Result<Self::Ok, Self::Error> {
Ok(self.text)
}
}

View File

@ -30,7 +30,7 @@ fn check_add_documents_with_primary_key_param() {
let update_id = response["updateId"].as_u64().unwrap(); let update_id = response["updateId"].as_u64().unwrap();
server.wait_update_id(update_id); server.wait_update_id(update_id);
// 3 - Check update sucess // 3 - Check update success
let (response, status_code) = server.get_update_status(update_id); let (response, status_code) = server.get_update_status(update_id);
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
@ -70,7 +70,7 @@ fn check_add_documents_with_nested_boolean() {
let update_id = response["updateId"].as_u64().unwrap(); let update_id = response["updateId"].as_u64().unwrap();
server.wait_update_id(update_id); server.wait_update_id(update_id);
// 3 - Check update sucess // 3 - Check update success
let (response, status_code) = server.get_update_status(update_id); let (response, status_code) = server.get_update_status(update_id);
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
@ -105,9 +105,56 @@ fn check_add_documents_with_nested_null() {
let update_id = response["updateId"].as_u64().unwrap(); let update_id = response["updateId"].as_u64().unwrap();
server.wait_update_id(update_id); server.wait_update_id(update_id);
// 3 - Check update sucess // 3 - Check update success
let (response, status_code) = server.get_update_status(update_id); let (response, status_code) = server.get_update_status(update_id);
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_eq!(response["status"], "processed"); assert_eq!(response["status"], "processed");
} }
// Test issue https://github.com/meilisearch/MeiliSearch/issues/574
#[test]
fn check_add_documents_with_nested_sequence() {
let mut server = common::Server::with_uid("tasks");
// 1 - Create the index with no primary_key
let body = json!({ "uid": "tasks" });
let (response, status_code) = server.create_index(body);
assert_eq!(status_code, 201);
assert_eq!(response["primaryKey"], json!(null));
// 2 - Add a document that contains a seq in a nested object
let body = json!([{
"id": 0,
"foo": {
"bar": [123],
"fez": [{
"id": 255,
"baz": "leesz",
"fuzz": {
"fax": [234]
}
}]
}
}]);
let url = "/indexes/tasks/documents";
let (response, status_code) = server.post_request(&url, body.clone());
eprintln!("{:#?}", response);
assert_eq!(status_code, 202);
let update_id = response["updateId"].as_u64().unwrap();
server.wait_update_id(update_id);
// 3 - Check update success
let (response, status_code) = server.get_update_status(update_id);
assert_eq!(status_code, 200);
assert_eq!(response["status"], "processed");
let url = "/indexes/tasks/search?q=leesz";
let (response, status_code) = server.get_request(&url);
assert_eq!(status_code, 200);
assert_eq!(response["hits"], body);
}