Skip to content

Commit b2b430c

Browse files
author
Liubov Didkivska
authored
Return error if json parsing fail (#1032)
Json parser returns error if parse fail. Add test to check parsing fails with invalid json. Change parsing in core, read, write libs. Relates-To: OLPEDGE-2147 Signed-off-by: Liubov Didkivska <ext-liubov.didkivska@here.com>
1 parent a5c6bf4 commit b2b430c

31 files changed

+302
-118
lines changed

olp-cpp-sdk-core/include/olp/core/generated/parser/JsonParser.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,24 @@ inline T parse(const std::string& json) {
4141
}
4242

4343
template <typename T>
44-
inline T parse(std::stringstream& json_stream) {
44+
inline T parse(std::stringstream& json_stream, bool& res) {
45+
res = false;
4546
rapidjson::Document doc;
4647
rapidjson::IStreamWrapper stream(json_stream);
4748
doc.ParseStream(stream);
4849
T result{};
4950
if (doc.IsObject() || doc.IsArray()) {
5051
from_json(doc, result);
52+
res = true;
5153
}
5254
return result;
5355
}
5456

55-
} // namespace parser
57+
template <typename T>
58+
inline T parse(std::stringstream& json_stream) {
59+
bool res = true;
60+
return parse<T>(json_stream, res);
61+
}
5662

63+
} // namespace parser
5764
} // namespace olp
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (C) 2020 HERE Europe B.V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
#pragma once
21+
22+
#include <sstream>
23+
#include <string>
24+
#include <utility>
25+
26+
#include <olp/core/client/ApiError.h>
27+
#include <olp/core/generated/parser/JsonParser.h>
28+
#include <olp/core/logging/Log.h>
29+
30+
namespace olp {
31+
namespace parser {
32+
33+
template <typename OutputResult,
34+
typename ParsingType = typename OutputResult::ResultType,
35+
typename... AdditionalArgs>
36+
OutputResult parse_result(std::stringstream& json_stream,
37+
const AdditionalArgs&... args) {
38+
bool res = true;
39+
auto obj = parse<ParsingType>(json_stream, res);
40+
41+
if (res) {
42+
return OutputResult({std::move(obj), args...});
43+
} else {
44+
return {
45+
client::ApiError(client::ErrorCode::Unknown, "Fail parsing response.")};
46+
}
47+
}
48+
49+
} // namespace parser
50+
} // namespace olp

olp-cpp-sdk-core/src/client/api/PlatformApi.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include <olp/core/http/NetworkUtils.h>
3131
// clang-format off
3232
#include "client/parser/ApiParser.h"
33-
#include <olp/core/generated/parser/JsonParser.h>
33+
#include "JsonResultParser.h"
3434
// clang-format on
3535

3636
namespace {
@@ -75,8 +75,8 @@ PlatformApi::ApisResponse PlatformApi::GetApis(
7575
return {{response.status, response.response.str()}};
7676
}
7777

78-
return {
79-
{parser::parse<Apis>(response.response), GetExpiry(response.headers)}};
78+
return parser::parse_result<ApisResponse, Apis>(response.response,
79+
GetExpiry(response.headers));
8080
}
8181

8282
CancellationToken PlatformApi::GetApis(const OlpClient& client,
@@ -90,8 +90,8 @@ CancellationToken PlatformApi::GetApis(const OlpClient& client,
9090
if (response.status != olp::http::HttpStatusCode::OK) {
9191
callback({{response.status, response.response.str()}});
9292
} else {
93-
callback({{parser::parse<Apis>(response.response),
94-
GetExpiry(response.headers)}});
93+
callback(parser::parse_result<ApisResponse, Apis>(
94+
response.response, GetExpiry(response.headers)));
9595
}
9696
};
9797

olp-cpp-sdk-core/src/client/api/PlatformApi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class CancellationContext;
3838
class PlatformApi {
3939
public:
4040
using ApisResult = std::pair<Apis, boost::optional<time_t>>;
41-
using ApisResponse = ApiResponse<ApisResult, client::ApiError>;
41+
using ApisResponse = ApiResponse<ApisResult, ApiError>;
4242
using ApisCallback = std::function<void(ApisResponse)>;
4343

4444
/**

olp-cpp-sdk-core/src/client/api/ResourcesApi.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include <olp/core/http/NetworkUtils.h>
2929
// clang-format off
3030
#include "client/parser/ApiParser.h"
31-
#include <olp/core/generated/parser/JsonParser.h>
31+
#include "JsonResultParser.h"
3232
// clang-format on
3333

3434
namespace {
@@ -75,8 +75,8 @@ ResourcesApi::ApisResponse ResourcesApi::GetApis(
7575
return {{response.status, response.response.str()}};
7676
}
7777

78-
return {
79-
{parser::parse<Apis>(response.response), GetExpiry(response.headers)}};
78+
return parser::parse_result<ApisResponse, Apis>(response.response,
79+
GetExpiry(response.headers));
8080
}
8181

8282
CancellationToken ResourcesApi::GetApis(const OlpClient& client,
@@ -92,8 +92,8 @@ CancellationToken ResourcesApi::GetApis(const OlpClient& client,
9292
if (response.status != olp::http::HttpStatusCode::OK) {
9393
callback({{response.status, response.response.str()}});
9494
} else {
95-
callback({{parser::parse<Apis>(response.response),
96-
GetExpiry(response.headers)}});
95+
callback(parser::parse_result<ApisResponse, Apis>(
96+
response.response, GetExpiry(response.headers)));
9797
}
9898
};
9999
return client.CallApi(resource_url, "GET", {}, header_params, {}, nullptr, "",

olp-cpp-sdk-core/src/client/api/ResourcesApi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class CancellationContext;
3838
class ResourcesApi {
3939
public:
4040
using ApisResult = std::pair<Apis, boost::optional<time_t>>;
41-
using ApisResponse = client::ApiResponse<ApisResult, client::ApiError>;
41+
using ApisResponse = ApiResponse<ApisResult, ApiError>;
4242
using ApisCallback = std::function<void(ApisResponse)>;
4343

4444
/**
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (C) 2020 HERE Europe B.V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
#pragma once
21+
22+
#include <sstream>
23+
#include <string>
24+
#include <utility>
25+
26+
#include <olp/core/client/ApiError.h>
27+
#include <olp/core/generated/parser/JsonParser.h>
28+
#include <olp/core/logging/Log.h>
29+
30+
namespace olp {
31+
namespace parser {
32+
33+
template <typename OutputResult,
34+
typename ParsingType = typename OutputResult::ResultType,
35+
typename... AdditionalArgs>
36+
OutputResult parse_result(std::stringstream& json_stream,
37+
const AdditionalArgs&... args) {
38+
bool res = true;
39+
auto obj = parse<ParsingType>(json_stream, res);
40+
41+
if (res) {
42+
return OutputResult({std::move(obj), args...});
43+
} else {
44+
return {
45+
client::ApiError(client::ErrorCode::Unknown, "Fail parsing response.")};
46+
}
47+
}
48+
49+
} // namespace parser
50+
} // namespace olp

olp-cpp-sdk-dataservice-read/src/generated/api/ConfigApi.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,15 @@
2626
#include <olp/core/client/OlpClient.h>
2727
// clang-format off
2828
#include "generated/parser/CatalogParser.h"
29-
#include <olp/core/generated/parser/JsonParser.h>
29+
#include "JsonResultParser.h"
3030
// clang-format on
3131

3232
namespace olp {
3333
namespace dataservice {
3434
namespace read {
35-
using namespace olp::client;
3635

3736
ConfigApi::CatalogResponse ConfigApi::GetCatalog(
38-
const OlpClient& client, const std::string& catalog_hrn,
37+
const client::OlpClient& client, const std::string& catalog_hrn,
3938
boost::optional<std::string> billing_tag,
4039
client::CancellationContext context) {
4140
std::multimap<std::string, std::string> header_params;
@@ -52,7 +51,7 @@ ConfigApi::CatalogResponse ConfigApi::GetCatalog(
5251
if (response.status != olp::http::HttpStatusCode::OK) {
5352
return client::ApiError(response.status, response.response.str());
5453
}
55-
return olp::parser::parse<model::Catalog>(response.response);
54+
return parser::parse_result<ConfigApi::CatalogResponse>(response.response);
5655
}
5756

5857
} // namespace read

olp-cpp-sdk-dataservice-read/src/generated/api/MetadataApi.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "generated/parser/PartitionsParser.h"
3232
#include "generated/parser/VersionResponseParser.h"
3333
#include "generated/parser/VersionInfosParser.h"
34-
#include <olp/core/generated/parser/JsonParser.h>
34+
#include "JsonResultParser.h"
3535
// clang-format on
3636

3737
namespace {
@@ -77,8 +77,7 @@ MetadataApi::LayerVersionsResponse MetadataApi::GetLayerVersions(
7777
return client::ApiError(api_response.status, api_response.response.str());
7878
}
7979

80-
return LayerVersionsResponse(
81-
olp::parser::parse<model::LayerVersions>(api_response.response));
80+
return parser::parse_result<LayerVersionsResponse>(api_response.response);
8281
}
8382

8483
MetadataApi::PartitionsResponse MetadataApi::GetPartitions(
@@ -115,8 +114,7 @@ MetadataApi::PartitionsResponse MetadataApi::GetPartitions(
115114
return {{api_response.status, api_response.response.str()}};
116115
}
117116

118-
return PartitionsResponse(
119-
olp::parser::parse<model::Partitions>(api_response.response));
117+
return parser::parse_result<PartitionsResponse>(api_response.response);
120118
}
121119

122120
MetadataApi::CatalogVersionResponse MetadataApi::GetLatestCatalogVersion(
@@ -140,8 +138,8 @@ MetadataApi::CatalogVersionResponse MetadataApi::GetLatestCatalogVersion(
140138
if (api_response.status != http::HttpStatusCode::OK) {
141139
return {{api_response.status, api_response.response.str()}};
142140
}
143-
return CatalogVersionResponse(
144-
olp::parser::parse<model::VersionResponse>(api_response.response));
141+
142+
return parser::parse_result<CatalogVersionResponse>(api_response.response);
145143
}
146144

147145
MetadataApi::VersionsResponse MetadataApi::ListVersions(
@@ -166,8 +164,7 @@ MetadataApi::VersionsResponse MetadataApi::ListVersions(
166164
if (api_response.status != http::HttpStatusCode::OK) {
167165
return {{api_response.status, api_response.response.str()}};
168166
}
169-
return VersionsResponse(
170-
olp::parser::parse<model::VersionInfos>(api_response.response));
167+
return parser::parse_result<VersionsResponse>(api_response.response);
171168
}
172169

173170
} // namespace read

olp-cpp-sdk-dataservice-read/src/generated/api/PlatformApi.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include <olp/core/client/OlpClient.h>
3030
// clang-format off
3131
#include "generated/parser/ApiParser.h"
32-
#include <olp/core/generated/parser/JsonParser.h>
32+
#include "JsonResultParser.h"
3333
// clang-format on
3434

3535
namespace olp {
@@ -53,8 +53,7 @@ PlatformApi::ApisResponse PlatformApi::GetApis(
5353
return ApisResponse(
5454
client::ApiError(response.status, response.response.str()));
5555
}
56-
return ApisResponse(
57-
parser::parse<olp::dataservice::read::model::Apis>(response.response));
56+
return parser::parse_result<ApisResponse>(response.response);
5857
}
5958
} // namespace read
6059
} // namespace dataservice

0 commit comments

Comments
 (0)