From b3f248bd934935d6b34ccd2439b19a639ed1fb86 Mon Sep 17 00:00:00 2001 From: Kishore Nallan Date: Tue, 8 Aug 2023 20:24:15 +0530 Subject: [PATCH] Handle emplace + null values. --- src/validator.cpp | 2 +- test/collection_nested_fields_test.cpp | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/validator.cpp b/src/validator.cpp index 51a7d19c..f814c923 100644 --- a/src/validator.cpp +++ b/src/validator.cpp @@ -626,7 +626,7 @@ Option validator_t::validate_index_in_memory(nlohmann::json& document, continue; } - if((a_field.optional || op == UPDATE || op == EMPLACE) && document.count(field_name) == 0) { + if((a_field.optional || op == UPDATE || (op == EMPLACE && is_update)) && document.count(field_name) == 0) { continue; } diff --git a/test/collection_nested_fields_test.cpp b/test/collection_nested_fields_test.cpp index 6def5d1a..98a94f37 100644 --- a/test/collection_nested_fields_test.cpp +++ b/test/collection_nested_fields_test.cpp @@ -2578,6 +2578,19 @@ TEST_F(CollectionNestedFieldsTest, EmplaceWithNullValueOnRequiredField) { ASSERT_TRUE(op.ok()); Collection *coll1 = op.get(); + auto doc_with_null = R"({ + "id": "0", + "currency": { + "eu": null + } + })"_json; + + auto add_op = coll1->add(doc_with_null.dump(), EMPLACE); + ASSERT_FALSE(add_op.ok()); + + add_op = coll1->add(doc_with_null.dump(), CREATE); + ASSERT_FALSE(add_op.ok()); + auto doc1 = R"({ "id": "0", "currency": { @@ -2585,7 +2598,7 @@ TEST_F(CollectionNestedFieldsTest, EmplaceWithNullValueOnRequiredField) { } })"_json; - auto add_op = coll1->add(doc1.dump(), CREATE); + add_op = coll1->add(doc1.dump(), CREATE); ASSERT_TRUE(add_op.ok()); // now update with null value -- should not be allowed