Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences
C++ algoliadatastoreelasticsearchenterprise-searchfacetingfull-text-searchfuzzy-searchgeosearchin-memoryinstantsearchmerchandisingpineconesearchsearch-enginesemantic-searchsimilarity-searchsite-searchsynonymstypo-tolerancevector-search
Ensure that ID in the path and body match.
Kishore Nallanf836a633b655
diff --git a/src/collection.cpp b/src/collection.cppindex d8a58bdf..ae0c68fb 100644--- a/src/collection.cpp+++ b/src/collection.cpp@@ -111,13 +111,19 @@ Option<doc_seq_id_t> Collection::to_doc(const std::string & json_str, nlohmann::return Option<doc_seq_id_t>(400, "Bad JSON: not a properly formed document.");}- // operation could be: insert, upsert or delete and we have to validate based on that+ if(document.count("id") != 0 && id != "" && document["id"] != id) {+ return Option<doc_seq_id_t>(400, "The `id` of the resource does not match the `id` on the JSON body.");+ }if(document.count("id") == 0 && !id.empty()) {// use the explicit ID (usually from a PUT request) if document body does not have itdocument["id"] = id;}+ if(document.count("id") != 0 && document["id"] == "") {+ return Option<doc_seq_id_t>(400, "The `id` should not be empty.");+ }+if(document.count("id") == 0) {if(operation == UPDATE) {return Option<doc_seq_id_t>(400, "For update, the `id` key must be provided.");diff --git a/test/collection_test.cpp b/test/collection_test.cppindex bb79807d..ba8ff13a 100644--- a/test/collection_test.cpp+++ b/test/collection_test.cpp@@ -2585,7 +2585,6 @@ TEST_F(CollectionTest, UpdateDocument) {doc4["points"] = 105;add_op = coll1->add(doc4.dump(), UPSERT, "100");- LOG(INFO) << add_op.error();ASSERT_TRUE(add_op.ok());res = coll1->search("*", {"tags"}, "points: > 101", {}, sort_fields, 0, 10, 1,@@ -2606,6 +2605,30 @@ TEST_F(CollectionTest, UpdateDocument) {ASSERT_EQ(1, res["hits"].size());ASSERT_EQ(105, res["hits"][0]["document"]["points"].get<size_t>());++ // when explicit path id does not match doc id, error should be returned+ nlohmann::json doc5;+ doc5["id"] = "800";+ doc5["title"] = "The Secret Seven";+ doc5["points"] = 250;+ doc5["tags"] = {"BOOK", "ENID BLYTON"};++ add_op = coll1->add(doc5.dump(), UPSERT, "799");+ ASSERT_FALSE(add_op.ok());+ ASSERT_EQ(400, add_op.code());+ ASSERT_STREQ("The `id` of the resource does not match the `id` on the JSON body.", add_op.error().c_str());++ // passing an empty id should not succeed+ nlohmann::json doc6;+ doc6["id"] = "";+ doc6["title"] = "The Secret Seven";+ doc6["points"] = 250;+ doc6["tags"] = {"BOOK", "ENID BLYTON"};++ add_op = coll1->add(doc6.dump(), UPDATE);+ ASSERT_FALSE(add_op.ok());+ ASSERT_EQ(400, add_op.code());+ ASSERT_STREQ("The `id` should not be empty.", add_op.error().c_str());}TEST_F(CollectionTest, SearchHighlightFieldFully) {