Make sur that we do not accept floats as documents ids

This commit is contained in:
Kerollmops 2022-06-15 16:06:52 +02:00
parent 2ceeb51c37
commit 19eb3b4708
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
2 changed files with 48 additions and 1 deletions

View File

@ -1987,4 +1987,51 @@ mod tests {
assert_eq!(ids.len(), map.len()); assert_eq!(ids.len(), map.len());
} }
#[test]
fn primary_key_must_not_contain_floats() {
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": -228142,
"title": "asdsad",
}]};
let doc2 = documents! {[{
"id": 228143.56,
"title": "something",
}]};
let doc3 = documents! {[{
"id": -228143.56,
"title": "something",
}]};
let doc4 = documents! {[{
"id": 2.0,
"title": "something",
}]};
let (builder, user_error) = builder.add_documents(doc1).unwrap();
user_error.unwrap();
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());
}
} }

View File

@ -108,7 +108,7 @@ pub fn validate_document_id_from_json(bytes: &[u8]) -> Result<StdResult<String,
return Ok(Err(UserError::InvalidDocumentId { document_id: Value::String(string) })) return Ok(Err(UserError::InvalidDocumentId { document_id: Value::String(string) }))
} }
}, },
Value::Number(number) => Ok(Ok(number.to_string())), Value::Number(number) if number.is_i64() => Ok(Ok(number.to_string())),
content => return Ok(Err(UserError::InvalidDocumentId { document_id: content.clone() })), content => return Ok(Err(UserError::InvalidDocumentId { document_id: content.clone() })),
} }
} }