Ensure that emplace allows non-optional field update.

This commit is contained in:
Kishore Nallan 2022-02-03 16:16:56 +05:30
parent 302f8a34e8
commit 31503f699f
2 changed files with 5 additions and 5 deletions

View File

@ -296,7 +296,7 @@ Option<uint32_t> Index::validate_index_in_memory(nlohmann::json& document, uint3
bool missing_default_sort_field = (!default_sorting_field.empty() && document.count(default_sorting_field) == 0);
if(op != UPDATE && missing_default_sort_field) {
if((op != UPDATE && op != EMPLACE) && missing_default_sort_field) {
return Option<>(400, "Field `" + default_sorting_field + "` has been declared as a default sorting field, "
"but is not found in the document.");
}
@ -309,7 +309,7 @@ Option<uint32_t> Index::validate_index_in_memory(nlohmann::json& document, uint3
continue;
}
if((a_field.optional || op == UPDATE) && document.count(field_name) == 0) {
if((a_field.optional || op == UPDATE || op == EMPLACE) && document.count(field_name) == 0) {
continue;
}
@ -320,7 +320,7 @@ Option<uint32_t> Index::validate_index_in_memory(nlohmann::json& document, uint3
if(a_field.optional && document[field_name].is_null()) {
// we will ignore `null` on an option field
if(op != UPDATE) {
if(op != UPDATE && op != EMPLACE) {
// for updates, the erasure is done later since we need to keep the key for overwrite
document.erase(field_name);
}

View File

@ -1250,7 +1250,7 @@ TEST_F(CollectionTest, ImportDocumentsEmplace) {
Collection* coll1;
std::vector<field> fields = {
field("title", field_types::STRING, false, false),
field("points", field_types::INT32, false, true)
field("points", field_types::INT32, false, false)
};
coll1 = collectionManager.get_collection("coll1").get();
@ -1328,7 +1328,7 @@ TEST_F(CollectionTest, ImportDocumentsEmplace) {
ASSERT_EQ(1, import_results[1].size());
ASSERT_EQ(1, import_results[1].size());
// can update individual document via "emplace"
// can update individual document via "emplace" with only partial field (missing points)
std::string doc_3_update = R"({"id": "3", "title": "The Superman"})";
auto add_op = coll1->add(doc_3_update, EMPLACE);
ASSERT_TRUE(add_op.ok());