mirror of
				https://github.com/meilisearch/meilisearch.git
				synced 2025-11-04 09:56:28 +00:00 
			
		
		
		
	Make sur that we do not accept floats as documents ids
This commit is contained in:
		@@ -1987,4 +1987,51 @@ mod tests {
 | 
			
		||||
 | 
			
		||||
        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());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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) }))
 | 
			
		||||
            }
 | 
			
		||||
        },
 | 
			
		||||
        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() })),
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user