feat(http): accept empty document additions

wip
This commit is contained in:
mpostma 2022-01-13 12:30:35 +01:00
parent 010dcc3e80
commit d263f762bf
3 changed files with 10 additions and 29 deletions

View File

@ -710,20 +710,11 @@ async fn replace_document() {
} }
#[actix_rt::test] #[actix_rt::test]
async fn error_add_no_documents() { async fn add_no_documents() {
let server = Server::new().await; let server = Server::new().await;
let index = server.index("test"); let index = server.index("test");
let (response, code) = index.add_documents(json!([]), None).await; let (_response, code) = index.add_documents(json!([]), None).await;
assert_eq!(code, 202);
let expected_response = json!({
"message": "The `json` payload must contain at least one document.",
"code": "malformed_payload",
"type": "invalid_request",
"link": "https://docs.meilisearch.com/errors#malformed_payload"
});
assert_eq!(response, expected_response);
assert_eq!(code, 400);
} }
#[actix_rt::test] #[actix_rt::test]

View File

@ -32,8 +32,6 @@ pub enum DocumentFormatError {
Box<dyn std::error::Error + Send + Sync + 'static>, Box<dyn std::error::Error + Send + Sync + 'static>,
PayloadType, PayloadType,
), ),
#[error("The `{0}` payload must contain at least one document.")]
EmptyPayload(PayloadType),
} }
impl From<(PayloadType, milli::documents::Error)> for DocumentFormatError { impl From<(PayloadType, milli::documents::Error)> for DocumentFormatError {
@ -50,7 +48,6 @@ impl ErrorCode for DocumentFormatError {
match self { match self {
DocumentFormatError::Internal(_) => Code::Internal, DocumentFormatError::Internal(_) => Code::Internal,
DocumentFormatError::MalformedPayload(_, _) => Code::MalformedPayload, DocumentFormatError::MalformedPayload(_, _) => Code::MalformedPayload,
DocumentFormatError::EmptyPayload(_) => Code::MalformedPayload,
} }
} }
} }
@ -63,10 +60,6 @@ pub fn read_csv(input: impl Read, writer: impl Write + Seek) -> Result<usize> {
let builder = let builder =
DocumentBatchBuilder::from_csv(input, writer).map_err(|e| (PayloadType::Csv, e))?; DocumentBatchBuilder::from_csv(input, writer).map_err(|e| (PayloadType::Csv, e))?;
if builder.len() == 0 {
return Err(DocumentFormatError::EmptyPayload(PayloadType::Csv));
}
let count = builder.finish().map_err(|e| (PayloadType::Csv, e))?; let count = builder.finish().map_err(|e| (PayloadType::Csv, e))?;
Ok(count) Ok(count)
@ -81,16 +74,17 @@ pub fn read_ndjson(input: impl Read, writer: impl Write + Seek) -> Result<usize>
let mut buf = String::new(); let mut buf = String::new();
while reader.read_line(&mut buf)? > 0 { while reader.read_line(&mut buf)? > 0 {
// skip empty lines
if buf == "\n" {
buf.clear();
continue;
}
builder builder
.extend_from_json(Cursor::new(&buf.as_bytes())) .extend_from_json(Cursor::new(&buf.as_bytes()))
.map_err(|e| (PayloadType::Ndjson, e))?; .map_err(|e| (PayloadType::Ndjson, e))?;
buf.clear(); buf.clear();
} }
if builder.len() == 0 {
return Err(DocumentFormatError::EmptyPayload(PayloadType::Ndjson));
}
let count = builder.finish().map_err(|e| (PayloadType::Ndjson, e))?; let count = builder.finish().map_err(|e| (PayloadType::Ndjson, e))?;
Ok(count) Ok(count)
@ -104,10 +98,6 @@ pub fn read_json(input: impl Read, writer: impl Write + Seek) -> Result<usize> {
.extend_from_json(input) .extend_from_json(input)
.map_err(|e| (PayloadType::Json, e))?; .map_err(|e| (PayloadType::Json, e))?;
if builder.len() == 0 {
return Err(DocumentFormatError::EmptyPayload(PayloadType::Json));
}
let count = builder.finish().map_err(|e| (PayloadType::Json, e))?; let count = builder.finish().map_err(|e| (PayloadType::Json, e))?;
Ok(count) Ok(count)

View File

@ -8,7 +8,7 @@ use indexmap::IndexMap;
use milli::documents::DocumentBatchReader; use milli::documents::DocumentBatchReader;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::document_formats::{read_ndjson, DocumentFormatError}; use crate::document_formats::read_ndjson;
use crate::index::update_handler::UpdateHandler; use crate::index::update_handler::UpdateHandler;
use crate::index::updates::apply_settings_to_builder; use crate::index::updates::apply_settings_to_builder;
@ -130,8 +130,8 @@ impl Index {
let empty = match read_ndjson(reader, &mut tmp_doc_file) { let empty = match read_ndjson(reader, &mut tmp_doc_file) {
// if there was no document in the file it's because the index was empty // if there was no document in the file it's because the index was empty
Ok(0) => true,
Ok(_) => false, Ok(_) => false,
Err(DocumentFormatError::EmptyPayload(_)) => true,
Err(e) => return Err(e.into()), Err(e) => return Err(e.into()),
}; };