From ae66d6a8d0f92db30ff08a01e5e5cb90d5f966a8 Mon Sep 17 00:00:00 2001 From: kishorenc Date: Fri, 12 Jun 2020 20:57:26 +0530 Subject: [PATCH] Schema response should contain optional field value. --- include/collection.h | 2 ++ src/collection.cpp | 23 +++++++++++++++++++++++ src/core_api.cpp | 31 ++++--------------------------- test/collection_test.cpp | 13 +++++++++++++ 4 files changed, 42 insertions(+), 27 deletions(-) diff --git a/include/collection.h b/include/collection.h index c1ebc55e..c821af1f 100644 --- a/include/collection.h +++ b/include/collection.h @@ -217,6 +217,8 @@ public: Option to_doc(const std::string & json_str, nlohmann::json & document); + nlohmann::json get_summary_json(); + Option add(const std::string & json_str); Option add_many(const std::string & json_str); diff --git a/src/collection.cpp b/src/collection.cpp index 2e7b30f2..c3342fec 100644 --- a/src/collection.cpp +++ b/src/collection.cpp @@ -127,6 +127,29 @@ Option Collection::to_doc(const std::string & json_str, nlohmann::json return Option(seq_id); } +nlohmann::json Collection::get_summary_json() { + nlohmann::json json_response; + + json_response["name"] = name; + json_response["num_documents"] = num_documents; + json_response["created_at"] = created_at; + + nlohmann::json fields_arr; + + for(const field & coll_field: fields) { + nlohmann::json field_json; + field_json[fields::name] = coll_field.name; + field_json[fields::type] = coll_field.type; + field_json[fields::facet] = coll_field.facet; + field_json[fields::optional] = coll_field.optional; + fields_arr.push_back(field_json); + } + + json_response["fields"] = fields_arr; + json_response["default_sorting_field"] = default_sorting_field; + return json_response; +} + Option Collection::add(const std::string & json_str) { nlohmann::json document; Option doc_seq_id_op = to_doc(json_str, document); diff --git a/src/core_api.cpp b/src/core_api.cpp index 56efeec7..4e47aeda 100644 --- a/src/core_api.cpp +++ b/src/core_api.cpp @@ -9,29 +9,6 @@ #include "system_metrics.h" #include "logger.h" -nlohmann::json collection_summary_json(Collection *collection) { - nlohmann::json json_response; - - json_response["name"] = collection->get_name(); - json_response["num_documents"] = collection->get_num_documents(); - json_response["created_at"] = collection->get_created_at(); - - const std::vector & coll_fields = collection->get_fields(); - nlohmann::json fields_arr; - - for(const field & coll_field: coll_fields) { - nlohmann::json field_json; - field_json[fields::name] = coll_field.name; - field_json[fields::type] = coll_field.type; - field_json[fields::facet] = coll_field.facet; - fields_arr.push_back(field_json); - } - - json_response["fields"] = fields_arr; - json_response["default_sorting_field"] = collection->get_default_sorting_field(); - return json_response; -} - bool handle_authentication(http_req& req, const route_path& rpath, const std::string& auth_key) { CollectionManager & collectionManager = CollectionManager::get_instance(); @@ -55,7 +32,7 @@ bool get_collections(http_req & req, http_res & res) { nlohmann::json json_response = nlohmann::json::array(); for(Collection* collection: collections) { - nlohmann::json collection_json = collection_summary_json(collection); + nlohmann::json collection_json = collection->get_summary_json(); json_response.push_back(collection_json); } @@ -150,7 +127,7 @@ bool post_create_collection(http_req & req, http_res & res) { collectionManager.create_collection(req_json["name"], fields, default_sorting_field); if(collection_op.ok()) { - nlohmann::json json_response = collection_summary_json(collection_op.get()); + nlohmann::json json_response = collection_op.get()->get_summary_json(); res.set_201(json_response.dump()); return true; } @@ -169,7 +146,7 @@ bool del_drop_collection(http_req & req, http_res & res) { return false; } - nlohmann::json collection_json = collection_summary_json(collection); + nlohmann::json collection_json = collection->get_summary_json(); Option drop_result = collectionManager.drop_collection(req.params["collection"]); if(!drop_result.ok()) { @@ -497,7 +474,7 @@ bool get_collection_summary(http_req & req, http_res & res) { return false; } - nlohmann::json json_response = collection_summary_json(collection); + nlohmann::json json_response = collection->get_summary_json(); res.set_200(json_response.dump()); return true; diff --git a/test/collection_test.cpp b/test/collection_test.cpp index 358e1a57..dfcfac39 100644 --- a/test/collection_test.cpp +++ b/test/collection_test.cpp @@ -2257,6 +2257,19 @@ TEST_F(CollectionTest, OptionalFields) { Option remove_op = coll1->remove("1"); ASSERT_TRUE(remove_op.ok()); + // try fetching the schema (should contain optional field) + nlohmann::json coll_summary = coll1->get_summary_json(); + LOG(INFO) << coll_summary; + ASSERT_STREQ("title", coll_summary["fields"][0]["name"].get().c_str()); + ASSERT_STREQ("string", coll_summary["fields"][0]["type"].get().c_str()); + ASSERT_FALSE(coll_summary["fields"][0]["facet"].get()); + ASSERT_FALSE(coll_summary["fields"][0]["optional"].get()); + + ASSERT_STREQ("description", coll_summary["fields"][1]["name"].get().c_str()); + ASSERT_STREQ("string", coll_summary["fields"][1]["type"].get().c_str()); + ASSERT_TRUE(coll_summary["fields"][1]["facet"].get()); + ASSERT_TRUE(coll_summary["fields"][1]["optional"].get()); + // default sorting field should not be declared optional fields = { field("title", field_types::STRING, false),