mirror of
https://github.com/typesense/typesense.git
synced 2025-05-21 22:33:27 +08:00
Fixed a bug where a document with a duplicate ID was being inserted without an error.
This commit is contained in:
parent
ba49971ddd
commit
85f9887a2f
@ -82,6 +82,12 @@ Option<nlohmann::json> Collection::add(const std::string & json_str) {
|
||||
}
|
||||
|
||||
std::string doc_id = document["id"];
|
||||
Option<nlohmann::json> doc_option = get(doc_id);
|
||||
|
||||
// we need to check if document ID already exists before attempting to index
|
||||
if(doc_option.ok()) {
|
||||
return Option<nlohmann::json>(400, std::string("A document with id ") + doc_id + " already exists.");
|
||||
}
|
||||
|
||||
const Option<uint32_t> & index_memory_op = index_in_memory(document, seq_id);
|
||||
|
||||
|
@ -1479,6 +1479,15 @@ TEST_F(CollectionTest, IndexingWithBadData) {
|
||||
ASSERT_FALSE(bad_token_ranking_field_op4.ok());
|
||||
ASSERT_STREQ("Bad JSON.", bad_token_ranking_field_op4.error().c_str());
|
||||
|
||||
// should return an error when a document with pre-existing id is being added
|
||||
std::string doc = "{\"id\": \"100\", \"name\": \"foo\", \"age\": 29, \"tags\": [], \"average\": 78}";
|
||||
Option<nlohmann::json> add_op = sample_collection->add(doc);
|
||||
ASSERT_TRUE(add_op.ok());
|
||||
add_op = sample_collection->add(doc);
|
||||
ASSERT_FALSE(add_op.ok());
|
||||
ASSERT_EQ(400, add_op.code());
|
||||
ASSERT_STREQ("A document with id 100 already exists.", add_op.error().c_str());
|
||||
|
||||
collectionManager.drop_collection("sample_collection");
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user