Skip to content

Commit 158d28c

Browse files
committed
FIX: Fix handling of null last_modified_date
1 parent 182a4d1 commit 158d28c

File tree

8 files changed

+38
-14
lines changed

8 files changed

+38
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
- "ICE Futures Europe (Commodities)" renamed to "ICE Europe Commodities"
1212

1313
### Bug fixes
14+
- Fixed handling of `null` `last_modified_date` in `MetadataGetDatasetCondition`
15+
response
1416
- Fixed default `ShouldLog` implementation
1517

1618
## 0.38.0 - 2025-06-10

include/databento/detail/json_helpers.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <nlohmann/json.hpp>
55

66
#include <map> // multimap
7+
#include <optional>
78
#include <string>
89
#include <string_view>
910
#include <vector>
@@ -79,6 +80,10 @@ template <>
7980
std::string ParseAt(std::string_view endpoint, const nlohmann::json& json,
8081
std::string_view key);
8182
template <>
83+
std::optional<std::string> ParseAt(std::string_view endpoint,
84+
const nlohmann::json& json,
85+
std::string_view key);
86+
template <>
8287
std::uint64_t ParseAt(std::string_view endpoint, const nlohmann::json& json,
8388
std::string_view key);
8489
template <>

include/databento/metadata.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <cstdint>
44
#include <map>
5+
#include <optional>
56
#include <ostream>
67
#include <string>
78
#include <unordered_map>
@@ -30,7 +31,7 @@ struct UnitPricesForMode {
3031
struct DatasetConditionDetail {
3132
std::string date;
3233
DatasetCondition condition;
33-
std::string last_modified_date;
34+
std::optional<std::string> last_modified_date;
3435
};
3536

3637
struct DatasetRange {

src/detail/json_helpers.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "databento/detail/json_helpers.hpp"
22

33
#include <numeric> // accumulate
4+
#include <optional>
45
#include <sstream> // istringstream
56
#include <string>
67

@@ -49,6 +50,17 @@ bool ParseAt(std::string_view endpoint, const nlohmann::json& json,
4950
template <>
5051
std::string ParseAt(std::string_view endpoint, const nlohmann::json& json,
5152
std::string_view key) {
53+
const auto s = ParseAt<std::optional<std::string>>(endpoint, json, key);
54+
if (s) {
55+
return *s;
56+
}
57+
return {};
58+
}
59+
60+
template <>
61+
std::optional<std::string> ParseAt(std::string_view endpoint,
62+
const nlohmann::json& json,
63+
std::string_view key) {
5264
const auto& val_json = CheckedAt(endpoint, json, key);
5365
if (val_json.is_null()) {
5466
return {};

src/historical.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -520,12 +520,10 @@ Historical::MetadataGetDatasetCondition(const httplib::Params& params) {
520520
if (!detail_json.is_object()) {
521521
throw JsonResponseError::TypeMismatch(kEndpoint, "object", detail_json);
522522
}
523-
std::string date =
524-
detail::ParseAt<std::string>(kEndpoint, detail_json, "date");
525-
const DatasetCondition condition =
526-
detail::FromCheckedAtString<DatasetCondition>(kEndpoint, detail_json,
527-
"condition");
528-
std::string last_modified_date = detail::ParseAt<std::string>(
523+
auto date = detail::ParseAt<std::string>(kEndpoint, detail_json, "date");
524+
const auto condition = detail::FromCheckedAtString<DatasetCondition>(
525+
kEndpoint, detail_json, "condition");
526+
auto last_modified_date = detail::ParseAt<std::optional<std::string>>(
529527
kEndpoint, detail_json, "last_modified_date");
530528
details.emplace_back(DatasetConditionDetail{std::move(date), condition,
531529
std::move(last_modified_date)});

src/stream_op_helper.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class StreamOpHelper {
6060
template <typename T>
6161
void FmtToStream(const std::optional<T>& val) {
6262
if (val.has_value()) {
63-
stream_ << *val;
63+
FmtToStream(*val);
6464
} else {
6565
stream_ << "nullopt";
6666
}

tests/src/historical_tests.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <cstdint>
1111
#include <cstdlib>
1212
#include <filesystem>
13+
#include <optional>
1314
#include <stdexcept> // logic_error
1415
#include <utility> // move
1516

@@ -424,7 +425,7 @@ TEST_F(HistoricalTests, TestMetadataGetDatasetCondition) {
424425
{"last_modified_date", "2023-03-01"}},
425426
{{"date", "2022-11-10"},
426427
{"condition", "missing"},
427-
{"last_modified_date", "2023-03-01"}}};
428+
{"last_modified_date", nullptr}}};
428429
mock_server_.MockGetJson("/v0/metadata.get_dataset_condition",
429430
{{"dataset", dataset::kXnasItch},
430431
{"start_date", "2022-11-06"},
@@ -440,7 +441,7 @@ TEST_F(HistoricalTests, TestMetadataGetDatasetCondition) {
440441
{"2022-11-07", DatasetCondition::Available, "2023-03-01"},
441442
{"2022-11-08", DatasetCondition::Degraded, "2023-03-01"},
442443
{"2022-11-09", DatasetCondition::Pending, "2023-03-01"},
443-
{"2022-11-10", DatasetCondition::Missing, "2023-03-01"},
444+
{"2022-11-10", DatasetCondition::Missing, std::nullopt},
444445
};
445446
EXPECT_EQ(res, kExp);
446447
}

tests/src/metadata_tests.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55

66
namespace databento::tests {
77
TEST(MetadataTests, TestDatasetConditionDetailToString) {
8-
const DatasetConditionDetail target{"2022-11-10", DatasetCondition::Available,
9-
"2023-03-01"};
10-
ASSERT_EQ(
11-
ToString(target),
8+
const DatasetConditionDetail available{
9+
"2022-11-10", DatasetCondition::Available, "2023-03-01"};
10+
EXPECT_EQ(
11+
ToString(available),
1212
R"(DatasetConditionDetail { date = "2022-11-10", condition = available, last_modified_date = "2023-03-01" })");
13+
const DatasetConditionDetail missing{
14+
"2022-11-11", DatasetCondition::Missing, {}};
15+
EXPECT_EQ(
16+
ToString(missing),
17+
R"(DatasetConditionDetail { date = "2022-11-11", condition = missing, last_modified_date = nullopt })");
1318
}
1419

1520
TEST(MetadataTests, TestDatasetRangeToString) {

0 commit comments

Comments
 (0)