Disallow use of type auto for a non-wildcard containing field.

This commit is contained in:
Kishore Nallan 2021-06-12 16:18:29 +05:30
parent 8dde664736
commit 0e967a9c44
2 changed files with 32 additions and 0 deletions

View File

@ -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<bool>(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<bool>(400, "Field `" + field.name + "` must be an optional field.");
@ -290,6 +297,15 @@ struct field {
field_json[fields::name].get<std::string>() + 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<std::string>().find(".*") == std::string::npos) {
return Option<bool>(400, std::string("Cannot use type `auto` for `") +
field_json[fields::name].get<std::string>() +
"`. 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<bool>(400, std::string("The `geo_resolution` property of the field `") +

View File

@ -532,6 +532,22 @@ TEST_F(CollectionAllFieldsTest, UpdateOfDocumentsInAutoMode) {
collectionManager.drop_collection("coll1");
}
TEST_F(CollectionAllFieldsTest, NormalFieldWithAutoType) {
Collection *coll1;
std::vector<field> 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;