From 4a3d2cc20961a28fa714bfaa80153db2516524b2 Mon Sep 17 00:00:00 2001 From: Kishore Nallan Date: Sat, 9 Oct 2021 18:14:19 +0530 Subject: [PATCH] Allow indexing of null values for auto/string* fields. --- src/collection.cpp | 7 +++++++ test/collection_all_fields_test.cpp | 30 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/collection.cpp b/src/collection.cpp index 35efdddf..838863e3 100644 --- a/src/collection.cpp +++ b/src/collection.cpp @@ -2272,6 +2272,13 @@ Option Collection::check_and_update_schema(nlohmann::json& document, const if(test_field_type == field_types::AUTO || field_types::is_string_or_array(test_field_type)) { parseable = field::get_type(kv.value(), field_type); if(!parseable) { + + if(kv.value().is_null() && new_field.optional) { + // null values are allowed only if field is optional + kv++; + continue; + } + if(dirty_values == DIRTY_VALUES::REJECT || dirty_values == DIRTY_VALUES::COERCE_OR_REJECT) { return Option(400, "Type of field `" + kv.key() + "` is invalid."); } else { diff --git a/test/collection_all_fields_test.cpp b/test/collection_all_fields_test.cpp index 7429ab8e..a2d3e91c 100644 --- a/test/collection_all_fields_test.cpp +++ b/test/collection_all_fields_test.cpp @@ -915,6 +915,36 @@ TEST_F(CollectionAllFieldsTest, DynamicFieldsMustOnlyBeOptional) { collectionManager.drop_collection("coll1"); } +TEST_F(CollectionAllFieldsTest, AutoAndStringStarFieldsShouldAcceptNullValues) { + Collection *coll1; + + std::vector fields = { + field("foo", "string*", true, true), + //field("buzz", "auto", true, true), + field("bar.*", "string*", true, true), + field("baz.*", "auto", true, true), + }; + + coll1 = collectionManager.get_collection("coll1").get(); + if (coll1 == nullptr) { + auto coll_op = collectionManager.create_collection("coll1", 1, fields, "", 0); + ASSERT_TRUE(coll_op.ok()); + coll1 = coll_op.get(); + } + + nlohmann::json doc; + doc["foo"] = nullptr; + //doc["buzz"] = nullptr; + doc["bar_one"] = nullptr; + doc["baz_one"] = nullptr; + + // should allow indexing of null values since all are optional + auto add_op = coll1->add(doc.dump(), CREATE); + ASSERT_TRUE(add_op.ok()); + + collectionManager.drop_collection("coll1"); +} + TEST_F(CollectionAllFieldsTest, BothFallbackAndDynamicFields) { Collection *coll1;