From ecab013f238735ab5df8d8192a3500e968a4cad4 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Tue, 9 May 2023 10:29:03 +0800 Subject: [PATCH 1/3] add CheckJsonFuc And Fix CheckUpdata BUG Signed-off-by: Jeremyzz --- .../src/executor/document/document_check.cpp | 10 +----- .../src/interface/src/document_store.cpp | 8 ++--- .../src/interface/src/result_set.cpp | 6 ++-- .../src/oh_adapter/include/json_object.h | 7 ++-- .../src/oh_adapter/src/json_object.cpp | 32 ++++++++++++++++--- .../unittest/api/documentdb_data_test.cpp | 2 +- 6 files changed, 41 insertions(+), 24 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp index d81d0c54..94bb5138 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp @@ -133,7 +133,7 @@ bool CheckCommon::CheckFilter(const std::string &filter, std::string &idStr, int return false; } - JsonObject filterObject = JsonObject::Parse(filter, errCode, true); + JsonObject filterObject = JsonObject::Parse(filter, errCode, true, true); if (errCode != E_OK) { GLOGE("Parse filter failed. %d", errCode); return false; @@ -256,14 +256,6 @@ int CheckCommon::CheckUpdata(JsonObject &updataObj, std::vector JSON_DEEP_MAX) { return -E_INVALID_ARGS;; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp index d639dc21..e880cbbd 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp @@ -159,7 +159,7 @@ int DocumentStore::UpdateDocument(const std::string &collection, const std::stri GLOGE("Check flags invalid."); return -E_INVALID_ARGS; } - JsonObject filterObj = JsonObject::Parse(filter, errCode, true); + JsonObject filterObj = JsonObject::Parse(filter, errCode, true, true); if (errCode != E_OK) { GLOGE("filter Parsed failed"); return errCode; @@ -251,7 +251,7 @@ int DocumentStore::UpsertDocument(const std::string &collection, const std::stri GLOGE("Check flags invalid."); return -E_INVALID_ARGS; } - JsonObject filterObj = JsonObject::Parse(filter, errCode, true); + JsonObject filterObj = JsonObject::Parse(filter, errCode, true, true); if (errCode != E_OK) { GLOGE("filter Parsed failed"); return errCode; @@ -404,7 +404,7 @@ int DocumentStore::DeleteDocument(const std::string &collection, const std::stri GLOGE("filter's length is too long"); return -E_OVER_LIMIT; } - JsonObject filterObj = JsonObject::Parse(filter, errCode, true); + JsonObject filterObj = JsonObject::Parse(filter, errCode, true, true); if (errCode != E_OK) { GLOGE("filter Parsed failed"); return errCode; @@ -460,7 +460,7 @@ int DocumentStore::FindDocument(const std::string &collection, const std::string GLOGE("args length is too long"); return -E_OVER_LIMIT; } - JsonObject filterObj = JsonObject::Parse(filter, errCode, true); + JsonObject filterObj = JsonObject::Parse(filter, errCode, true, true); if (errCode != E_OK) { GLOGE("filter Parsed failed"); return errCode; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set.cpp index bcd511a8..9dc8a360 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set.cpp @@ -59,7 +59,7 @@ int ResultSet::GetNext() if (!ifFiled_ && index_ == 0) { if (isOnlyId_) { int errCode = 0; - JsonObject filterObj = JsonObject::Parse(filter_, errCode, true); + JsonObject filterObj = JsonObject::Parse(filter_, errCode, true, true); if (errCode != E_OK) { GLOGE("filter Parsed failed"); return errCode; @@ -88,7 +88,7 @@ int ResultSet::GetNext() int errCode = 0; auto coll = Collection(collectionName_, store_->GetExecutor(errCode)); std::vector> values; - JsonObject filterObj = JsonObject::Parse(filter_, errCode, true); + JsonObject filterObj = JsonObject::Parse(filter_, errCode, true, true); if (errCode != E_OK) { GLOGE("filter Parsed failed"); return errCode; @@ -107,7 +107,7 @@ int ResultSet::GetNext() int errCode = 0; auto coll = Collection(collectionName_, store_->GetExecutor(errCode)); std::vector> values; - JsonObject filterObj = JsonObject::Parse(filter_, errCode, true); + JsonObject filterObj = JsonObject::Parse(filter_, errCode, true, true); if (errCode != E_OK) { GLOGE("filter Parsed failed"); return errCode; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/json_object.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/json_object.h index f4ebbdd3..3e38e89f 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/json_object.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/json_object.h @@ -60,7 +60,8 @@ using JsonFieldPath = std::vector; class JsonObject { public: - static JsonObject Parse(const std::string &jsonStr, int &errCode, bool caseSensitive = false); + static JsonObject Parse(const std::string &jsonStr, int &errCode, + bool caseSensitive = false, bool isFilter = false); bool operator==(const JsonObject& other) const; // If the two nodes exist with a different fieldName, then return 0. ~JsonObject(); @@ -104,11 +105,11 @@ public: private: JsonObject(); - int Init(const std::string &str); + int Init(const std::string &str, bool isFilter = false); int GetDeep(cJSON *cjson); int CheckNumber(cJSON *cjson, int &errCode); - + bool CheckJsonFormat(cJSON *cjson, int &errCode); cJSON *cjson_ = nullptr; int jsonDeep_ = 0; bool isOwner_ = false; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp index 8667b9da..ff045a39 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp @@ -14,7 +14,7 @@ */ #include "json_object.h" - +#include #include #include "doc_errno.h" @@ -103,10 +103,11 @@ bool ValueObject::operator==(const ValueObject &other) const return false; } -JsonObject JsonObject::Parse(const std::string &jsonStr, int &errCode, bool caseSensitive) +JsonObject JsonObject::Parse(const std::string &jsonStr, int &errCode, + bool caseSensitive, bool isFilter) { JsonObject obj; - errCode = obj.Init(jsonStr); + errCode = obj.Init(jsonStr, isFilter); obj.caseSensitive_ = caseSensitive; return obj; } @@ -191,7 +192,7 @@ int JsonObject::CheckNumber(cJSON *item, int &errCode) return E_OK; } -int JsonObject::Init(const std::string &str) +int JsonObject::Init(const std::string &str, bool isFilter) { const char *end = NULL; isOwner_ = true; @@ -212,9 +213,32 @@ int JsonObject::Init(const std::string &str) GLOGE("Int value is larger than double"); return -E_INVALID_ARGS; } + int errCode = E_OK; + if (!isFilter && !CheckJsonFormat(cjson_, errCode)) { + return -E_INVALID_JSON_FORMAT; + } return E_OK; } +bool JsonObject::CheckJsonFormat(cJSON *cjson, int &errCode) +{ + std::set filedSet; + cJSON *cjsonChild = cjson_->child; + while (cjsonChild != nullptr) { + std::string fieldName = cjsonChild->string; + if (fieldName.empty()) { + return false; + } + if (filedSet.find(fieldName) == filedSet.end()) { + filedSet.insert(fieldName); + } else { + return false; + } + cjsonChild = cjsonChild->next; + } + return true; +} + std::string JsonObject::Print() const { if (cjson_ == nullptr) { diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp index 5e256eb1..5b4d3795 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp @@ -296,7 +296,7 @@ HWTEST_F(DocumentDBDataTest, UpdateDataTest009, TestSize.Level0) { std::string filter = R""({"_id":"1234"})""; std::string document = R""({"_id":"1234", "field1":{"c_field":{"cc_field":{"ccc_field":1}}}, "field2" : 2})""; - + EXPECT_EQ(GRD_InsertDoc(g_db, g_coll, document.c_str(), 0), GRD_OK); std::string updata = R""({"field1":1, "FIELD1":[1, true, 1.23456789, "hello world!", null]})""; -- Gitee From bbc41c563f83799c4d2c58ce3b9246f335b8214c Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Tue, 9 May 2023 11:32:53 +0800 Subject: [PATCH 2/3] fix Code check opinion Signed-off-by: Jeremyzz --- .../src/oh_adapter/src/json_object.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp index ff045a39..d105e957 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp @@ -214,17 +214,19 @@ int JsonObject::Init(const std::string &str, bool isFilter) return -E_INVALID_ARGS; } int errCode = E_OK; - if (!isFilter && !CheckJsonFormat(cjson_, errCode)) { + if (!isFilter && !CheckJsonFormat(cjson_->child, errCode)) { return -E_INVALID_JSON_FORMAT; } return E_OK; } -bool JsonObject::CheckJsonFormat(cJSON *cjson, int &errCode) +bool JsonObject::CheckJsonFormat(cJSON *cjsonChild, int &errCode) { std::set filedSet; - cJSON *cjsonChild = cjson_->child; while (cjsonChild != nullptr) { + if (cjsonChild->string == nullptr) { + return true; + } std::string fieldName = cjsonChild->string; if (fieldName.empty()) { return false; @@ -234,6 +236,9 @@ bool JsonObject::CheckJsonFormat(cJSON *cjson, int &errCode) } else { return false; } + if (!CheckJsonFormat(cjsonChild->child, errCode)) { + return false; + } cjsonChild = cjsonChild->next; } return true; -- Gitee From d83274cb43f99fc198fc23e077dfc66653736005 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Tue, 9 May 2023 15:17:43 +0800 Subject: [PATCH 3/3] fix Code Check opinion Signed-off-by: Jeremyzz --- .../src/oh_adapter/include/json_object.h | 5 +- .../src/oh_adapter/src/json_object.cpp | 75 ++++++++++++++----- .../unittest/api/documentdb_data_test.cpp | 8 +- 3 files changed, 65 insertions(+), 23 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/json_object.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/json_object.h index 3e38e89f..69fc3d4e 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/json_object.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/json_object.h @@ -20,6 +20,7 @@ #include #include #include +#include #include "cJSON.h" @@ -106,10 +107,10 @@ public: private: JsonObject(); int Init(const std::string &str, bool isFilter = false); - + int CheckJsonRepeatField(cJSON *object); + int CheckSubObj(std::set &filedSet, cJSON *subObj, int parentType); int GetDeep(cJSON *cjson); int CheckNumber(cJSON *cjson, int &errCode); - bool CheckJsonFormat(cJSON *cjson, int &errCode); cJSON *cjson_ = nullptr; int jsonDeep_ = 0; bool isOwner_ = false; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp index d105e957..ed4d1169 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp @@ -14,7 +14,6 @@ */ #include "json_object.h" -#include #include #include "doc_errno.h" @@ -214,34 +213,70 @@ int JsonObject::Init(const std::string &str, bool isFilter) return -E_INVALID_ARGS; } int errCode = E_OK; - if (!isFilter && !CheckJsonFormat(cjson_->child, errCode)) { - return -E_INVALID_JSON_FORMAT; + if (!isFilter) { + ret = CheckJsonRepeatField(cjson_); + if (ret != E_OK) { + return ret; + } } return E_OK; } -bool JsonObject::CheckJsonFormat(cJSON *cjsonChild, int &errCode) +int JsonObject::CheckJsonRepeatField(cJSON *object) { + if (object == nullptr) { + return -E_INVALID_ARGS; + } + int ret = E_OK; + int type = object->type; + if (type != cJSON_Object && type != cJSON_Array) { + return ret; + } std::set filedSet; - while (cjsonChild != nullptr) { - if (cjsonChild->string == nullptr) { - return true; - } - std::string fieldName = cjsonChild->string; - if (fieldName.empty()) { - return false; - } - if (filedSet.find(fieldName) == filedSet.end()) { - filedSet.insert(fieldName); - } else { - return false; + cJSON *subObj = object->child; + while (subObj != nullptr) { + ret = CheckSubObj(filedSet, subObj, type); + if (ret != E_OK) { + break; } - if (!CheckJsonFormat(cjsonChild->child, errCode)) { - return false; + subObj = subObj->next; + } + return ret; +} + +int JsonObject::CheckSubObj(std::set &filedSet, cJSON *subObj, int parentType) +{ + if (subObj == nullptr) { + return -E_INVALID_ARGS; + } + int ret = E_OK; + std::string fieldName; + if (subObj->string != nullptr) { + fieldName = subObj->string; + } + if (parentType == cJSON_Array) { + ret = CheckJsonRepeatField(subObj); + if (ret != E_OK) { + return ret; } - cjsonChild = cjsonChild->next; + return E_OK; } - return true; + if (fieldName.empty()) { + return -E_INVALID_JSON_FORMAT; + } + if (filedSet.find(fieldName) == filedSet.end()) { + filedSet.insert(fieldName); + } else { + ret = -E_INVALID_JSON_FORMAT; + } + if (ret != E_OK) { + return ret; + } + ret = CheckJsonRepeatField(subObj); + if (ret != E_OK) { + return ret; + } + return E_OK; } std::string JsonObject::Print() const diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp index 5b4d3795..4ee5921c 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp @@ -180,7 +180,13 @@ HWTEST_F(DocumentDBDataTest, UpsertDataTest009, TestSize.Level0) HWTEST_F(DocumentDBDataTest, UpsertDataTest010, TestSize.Level0) { - int result = GRD_UpsertDoc(g_db, g_coll, R"({"_id" : "abcde"})", R"({"a00001":1, "a00001":2})", 0); + int result = GRD_UpsertDoc(g_db, g_coll, R"({"_id" : "abcde"})", R"({"a00001": {"A":1, "A":2}})", 0); + ASSERT_EQ(result, GRD_INVALID_FORMAT); +} + +HWTEST_F(DocumentDBDataTest, UpsertDataTest011, TestSize.Level0) +{ + int result = GRD_UpsertDoc(g_db, g_coll, R"({"_id" : "abcde"})", R"({"t1":{"t22":[1,{"t23":1, "t23":1},3 ,4]}})", 0); ASSERT_EQ(result, GRD_INVALID_FORMAT); } -- Gitee