Schema response should contain optional field value.

This commit is contained in:
kishorenc 2020-06-12 20:57:26 +05:30
parent 81dfe76009
commit ae66d6a8d0
4 changed files with 42 additions and 27 deletions

View File

@ -217,6 +217,8 @@ public:
Option<uint32_t> to_doc(const std::string & json_str, nlohmann::json & document);
nlohmann::json get_summary_json();
Option<nlohmann::json> add(const std::string & json_str);
Option<nlohmann::json> add_many(const std::string & json_str);

View File

@ -127,6 +127,29 @@ Option<uint32_t> Collection::to_doc(const std::string & json_str, nlohmann::json
return Option<uint32_t>(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<nlohmann::json> Collection::add(const std::string & json_str) {
nlohmann::json document;
Option<uint32_t> doc_seq_id_op = to_doc(json_str, document);

View File

@ -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<field> & 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<bool> 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;

View File

@ -2257,6 +2257,19 @@ TEST_F(CollectionTest, OptionalFields) {
Option<std::string> 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<std::string>().c_str());
ASSERT_STREQ("string", coll_summary["fields"][0]["type"].get<std::string>().c_str());
ASSERT_FALSE(coll_summary["fields"][0]["facet"].get<bool>());
ASSERT_FALSE(coll_summary["fields"][0]["optional"].get<bool>());
ASSERT_STREQ("description", coll_summary["fields"][1]["name"].get<std::string>().c_str());
ASSERT_STREQ("string", coll_summary["fields"][1]["type"].get<std::string>().c_str());
ASSERT_TRUE(coll_summary["fields"][1]["facet"].get<bool>());
ASSERT_TRUE(coll_summary["fields"][1]["optional"].get<bool>());
// default sorting field should not be declared optional
fields = {
field("title", field_types::STRING, false),