Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

fix: Remove whitespace trimming during document id validation #599

Merged
merged 1 commit into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions milli/src/update/index_documents/enrich.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,7 @@ pub fn fetch_matching_values_in_object(
}
}

/// Returns a trimmed version of the document id or `None` if it is invalid.
pub fn validate_document_id(document_id: &str) -> Option<&str> {
let document_id = document_id.trim();
if !document_id.is_empty()
&& document_id.chars().all(|c| matches!(c, 'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_'))
{
Expand Down
47 changes: 47 additions & 0 deletions milli/src/update/index_documents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2086,4 +2086,51 @@ mod tests {
let (_builder, user_error) = builder.add_documents(doc4).unwrap();
assert!(user_error.is_err());
}

#[test]
fn primary_key_must_not_contain_whitespace() {
let tmp = tempfile::tempdir().unwrap();
let mut options = EnvOpenOptions::new();
options.map_size(4096 * 100);
let index = Index::new(options, tmp).unwrap();
let mut wtxn = index.write_txn().unwrap();
let indexer_config = IndexerConfig::default();
let builder = IndexDocuments::new(
&mut wtxn,
&index,
&indexer_config,
IndexDocumentsConfig::default(),
|_| (),
)
.unwrap();

let doc1 = documents! {[{
"id": " 1",
"title": "asdsad",
}]};

let doc2 = documents! {[{
"id": "\t2",
"title": "something",
}]};

let doc3 = documents! {[{
"id": "\r3",
"title": "something",
}]};

let doc4 = documents! {[{
"id": "\n4",
"title": "something",
}]};

let (builder, user_error) = builder.add_documents(doc1).unwrap();
assert!(user_error.is_err());
let (builder, user_error) = builder.add_documents(doc2).unwrap();
assert!(user_error.is_err());
let (builder, user_error) = builder.add_documents(doc3).unwrap();
assert!(user_error.is_err());
let (_builder, user_error) = builder.add_documents(doc4).unwrap();
assert!(user_error.is_err());
}
}