From 52b4b50db064ad32e9a8b4eaadb4f6553c1a5f35 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 8 May 2023 17:28:19 +0800 Subject: [PATCH] empty updata bug fix Signed-off-by: Jeremyzz --- .../src/executor/document/document_check.cpp | 15 +++++++++------ .../src/executor/document/document_check.h | 2 +- .../src/interface/src/document_store.cpp | 14 ++++++++------ .../test/unittest/api/documentdb_data_test.cpp | 13 ++++++++++++- .../oh_adapter/documentdb_json_common_test.cpp | 15 +++++++++++++++ 5 files changed, 45 insertions(+), 14 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 285036fc..e94fcf9a 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 @@ -238,35 +238,38 @@ int CheckCommon::CheckDocument(JsonObject &documentObj) return E_OK; } -bool CheckCommon::CheckUpdata(JsonObject &updataObj, std::vector> &path) +int CheckCommon::CheckUpdata(JsonObject &updataObj, std::vector> &path) { if (updataObj.GetDeep() > JSON_DEEP_MAX) { GLOGE("projectionObj's json deep is deeper than JSON_DEEP_MAX"); return -E_INVALID_ARGS; } for (int i = 0; i < path.size(); i++) { + if (path[i].empty()) { + return -E_INVALID_JSON_FORMAT; + } for (int j = 0; j < path[i].size(); j++) { for (auto oneChar : path[i][j]) { if (!((isalpha(oneChar)) || (isdigit(oneChar)) || ('_' == oneChar))) { - return false; + return -E_INVALID_ARGS;; } } } if (!path[i].empty() && !path[i][0].empty() && isdigit(path[i][0][0])) { - return false; + return -E_INVALID_ARGS;; } } for (auto singlePath : path) { if (singlePath.size() > JSON_DEEP_MAX) { - return false; + return -E_INVALID_ARGS;; } } bool isIdExist = true; CheckIdFormat(updataObj, isIdExist); if (isIdExist) { - return false; + return -E_INVALID_ARGS;; } - return true; + return E_OK; } bool CheckCommon::CheckProjection(JsonObject &projectionObj, std::vector> &path) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.h index d094ed42..45d50bc1 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.h @@ -35,7 +35,7 @@ public: static int CheckIdFormat(JsonObject &data); static int CheckIdFormat(JsonObject &data, bool &isIdExisit); static int CheckDocument(JsonObject &document); - static bool CheckUpdata(JsonObject &updata, std::vector> &path); + static int CheckUpdata(JsonObject &updata, std::vector> &path); static bool CheckDocument(const std::string &updateStr, int &errCode); static bool CheckProjection(JsonObject &projectionObj, std::vector> &path); }; 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 210316d8..7511968d 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 @@ -133,9 +133,10 @@ int DocumentStore::UpdateDocument(const std::string &collection, const std::stri GLOGE("updateObj ParsePath faild"); return errCode; } - if (!CheckCommon::CheckUpdata(updateObj, allPath)) { - GLOGE("Updata format unvalid"); - return -E_INVALID_ARGS; + errCode = CheckCommon::CheckUpdata(updateObj, allPath); + if (errCode != E_OK) { + GLOGE("Updata format is illegal"); + return errCode; } } if (flags != GRD_DOC_APPEND && flags != GRD_DOC_REPLACE) { @@ -213,9 +214,10 @@ int DocumentStore::UpsertDocument(const std::string &collection, const std::stri if (errCode != E_OK) { return errCode; } - if (!CheckCommon::CheckUpdata(documentObj, allPath)) { - GLOGE("updata format unvalid"); - return -E_INVALID_ARGS; + errCode = CheckCommon::CheckUpdata(documentObj, allPath); + if (errCode != E_OK) { + GLOGE("UpsertDocument document format is illegal"); + return errCode; } } if (flags != GRD_DOC_APPEND && flags != GRD_DOC_REPLACE) { 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 17ff76b7..bddf3c40 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 @@ -22,6 +22,7 @@ #include "grd_document/grd_document_api.h" #include "log_print.h" #include "sqlite_utils.h" +#include "cJSON.h" using namespace DocumentDB; using namespace testing::ext; @@ -257,4 +258,14 @@ HWTEST_F(DocumentDBDataTest, UpdateDataTest006, TestSize.Level0) GLOGD("UpdateDataTest006: update data with flag: %u", flag); EXPECT_EQ(GRD_UpdateDoc(g_db, g_coll, filter.c_str(), document.c_str(), flag), GRD_INVALID_ARGS); } -} \ No newline at end of file +} + +HWTEST_F(DocumentDBDataTest, UpdateDataTest007, TestSize.Level0) +{ + int result = GRD_OK; + string doc = R"({"_id":"007", "field1":{"c_field":{"cc_field":{"ccc_field":1}}}, "field2":2})"; + result = GRD_InsertDoc(g_db, g_coll, doc.c_str(), 0); + EXPECT_EQ(result, GRD_OK); + result = GRD_UpdateDoc(g_db, g_coll, "{\"field2\" : 2}", "{\"\":3}", 0); + EXPECT_EQ(result, GRD_INVALID_FORMAT); +} diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp index 9c125f7c..10636149 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp @@ -600,4 +600,19 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest022, TestSize.Leve EXPECT_EQ(JsonCommon::IsJsonNodeMatch(srcObj1, filterObj, errCode), false); EXPECT_EQ(JsonCommon::IsJsonNodeMatch(srcObj2, filterObj, errCode), true); } + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest023, TestSize.Level0) +{ + std::string document = "{\"name\": 1, \"personInfo.school\": 1, \"personInfo.age\": 1}"; + int errCode = E_OK; + JsonObject srcObj = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + auto path = JsonCommon::ParsePath(srcObj, errCode); + for (auto singlePath : path) { + for (auto filedName : singlePath) { + GLOGE("filedName is =========>%s", filedName.c_str()); + } + GLOGE("///////////////////////////"); + } +} } \ No newline at end of file -- Gitee