Skip to content

Commit 3407715

Browse files
authored
Merge pull request #11 from Royna2544/copilot/fix-issue-6
Fix optional vector serialization in ReplyParameters
2 parents 62c0d51 + ce87aed commit 3407715

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

include/tgbot/types/ReplyParameters.h

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

66
#include <cstdint>
77
#include <memory>
8+
#include <optional>
89
#include <string>
910
#include <vector>
1011

@@ -60,7 +61,7 @@ class ReplyParameters {
6061
*
6162
* It can be specified instead of quoteParseMode.
6263
*/
63-
std::vector<MessageEntity::Ptr> quoteEntities;
64+
std::optional<std::vector<MessageEntity::Ptr>> quoteEntities;
6465

6566
/**
6667
* @brief Optional. Position of the quote in the original message in UTF-16 code units

src/TgTypeParser.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ struct JsonWrapper {
4747
}
4848
data_[std::string(key)] = *value;
4949
}
50+
template <typename T,
51+
std::enable_if_t<detail::is_vector_v<T>, bool> = true>
52+
void put(const std::string_view key, std::optional<T> value) {
53+
if (!value) {
54+
return; // Skip empty optional
55+
}
56+
data_[std::string(key)] = TgBot::put(*value);
57+
}
5058

5159
static void merge(nlohmann::json &thiz, const nlohmann::json &other) {
5260
if (!thiz.is_object() || !other.is_object()) {
@@ -818,7 +826,9 @@ DECLARE_PARSER_FROM_JSON(ReplyParameters) {
818826
&result->allowSendingWithoutReply);
819827
parse(data, "quote", &result->quote);
820828
parse(data, "quote_parse_mode", &result->quoteParseMode);
821-
result->quoteEntities = parseArray<MessageEntity>(data, "quote_entities");
829+
if (data.contains("quote_entities")) {
830+
result->quoteEntities = parseArray<MessageEntity>(data, "quote_entities");
831+
}
822832
parse(data, "quote_position", &result->quotePosition);
823833
return result;
824834
}
@@ -833,7 +843,7 @@ DECLARE_PARSER_TO_JSON(ReplyParameters) {
833843
object->allowSendingWithoutReply);
834844
json.put("quote", object->quote);
835845
json.put("quote_parse_mode", object->quoteParseMode);
836-
json.put("quote_entities", put(object->quoteEntities));
846+
json.put("quote_entities", object->quoteEntities);
837847
json.put("quote_position", object->quotePosition);
838848
}
839849

0 commit comments

Comments
 (0)