From 0e967a9c448257bac7fe82cbc2083e49dda1605e Mon Sep 17 00:00:00 2001 From: Kishore Nallan Date: Sat, 12 Jun 2021 16:18:29 +0530 Subject: [PATCH] Disallow use of type `auto` for a non-wildcard containing field. --- include/field.h | 16 ++++++++++++++++ test/collection_all_fields_test.cpp | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/include/field.h b/include/field.h index fc1d2b30..ae637dca 100644 --- a/include/field.h +++ b/include/field.h @@ -235,6 +235,13 @@ struct field { found_default_sorting_field = true; } + if(field.type == field_types::AUTO) { + if(field.name.find(".*") == std::string::npos) { + return Option(400, std::string("Cannot use type `auto` for `") + + field.name + "`. It can be used only for a field name containing `.*`"); + } + } + if(field.is_dynamic() && !field.optional) { if(field_types::is_string_or_array(field.type)) { return Option(400, "Field `" + field.name + "` must be an optional field."); @@ -290,6 +297,15 @@ struct field { field_json[fields::name].get() + std::string("` should be a boolean.")); } + // field of type auto can be used only on a field name containing .* + if(field_json.at(fields::type) == "auto") { + if(field_json.at(fields::name).get().find(".*") == std::string::npos) { + return Option(400, std::string("Cannot use type `auto` for `") + + field_json[fields::name].get() + + "`. It can be used only for a field name containing `.*`"); + } + } + if(field_json.count(fields::geo_resolution) != 0) { if(!field_json.at(fields::geo_resolution).is_number_integer()) { return Option(400, std::string("The `geo_resolution` property of the field `") + diff --git a/test/collection_all_fields_test.cpp b/test/collection_all_fields_test.cpp index 0c423cab..9f77ac27 100644 --- a/test/collection_all_fields_test.cpp +++ b/test/collection_all_fields_test.cpp @@ -532,6 +532,22 @@ TEST_F(CollectionAllFieldsTest, UpdateOfDocumentsInAutoMode) { collectionManager.drop_collection("coll1"); } +TEST_F(CollectionAllFieldsTest, NormalFieldWithAutoType) { + Collection *coll1; + + std::vector fields = {field("publication_year", field_types::AUTO, true)}; + + coll1 = collectionManager.get_collection("coll1").get(); + if (coll1 == nullptr) { + auto coll_op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO); + ASSERT_FALSE(coll_op.ok()); + ASSERT_EQ("Cannot use type `auto` for `publication_year`. It can be used only for a field name containing `.*`", + coll_op.error()); + } + + collectionManager.drop_collection("coll1"); +} + TEST_F(CollectionAllFieldsTest, JsonFieldsToFieldsConversion) { nlohmann::json fields_json = nlohmann::json::array(); nlohmann::json all_field;