From a52fe8eeb59294f571ac45e94232c8213a38eb52 Mon Sep 17 00:00:00 2001 From: LasterAlex Date: Fri, 30 Aug 2024 09:10:25 +0300 Subject: [PATCH 1/3] Fixed Vec serialization --- crates/teloxide-core/src/payloads/codegen.rs | 5 +++ .../src/payloads/copy_messages.rs | 1 + .../src/payloads/delete_messages.rs | 1 + .../src/payloads/forward_messages.rs | 1 + crates/teloxide-core/src/types.rs | 32 +++++++++++++++++++ 5 files changed, 40 insertions(+) diff --git a/crates/teloxide-core/src/payloads/codegen.rs b/crates/teloxide-core/src/payloads/codegen.rs index f3503a1b..03bb9957 100644 --- a/crates/teloxide-core/src/payloads/codegen.rs +++ b/crates/teloxide-core/src/payloads/codegen.rs @@ -223,6 +223,11 @@ fn params(params: impl Iterator>) -> String { { "\n #[serde(flatten)]" } + Type::ArrayOf(b) if **b == Type::RawTy("MessageId".to_string()) => + { + "\n #[serde(with = \ + \"crate::types::vec_msg_id_as_vec_int\")]" + } _ => "", }; let with = match ty { diff --git a/crates/teloxide-core/src/payloads/copy_messages.rs b/crates/teloxide-core/src/payloads/copy_messages.rs index 90a4cd90..87b6c915 100644 --- a/crates/teloxide-core/src/payloads/copy_messages.rs +++ b/crates/teloxide-core/src/payloads/copy_messages.rs @@ -17,6 +17,7 @@ impl_payload! { /// Unique identifier for the chat where the original message was sent (or channel username in the format `@channelusername`) pub from_chat_id: Recipient [into], /// Identifiers of 1-100 messages in the chat _from\_chat\_id_ to copy. The identifiers must be specified in a strictly increasing order. + #[serde(with = "crate::types::vec_msg_id_as_vec_int")] pub message_ids: Vec [collect], } optional { diff --git a/crates/teloxide-core/src/payloads/delete_messages.rs b/crates/teloxide-core/src/payloads/delete_messages.rs index f8306c03..e28e04ae 100644 --- a/crates/teloxide-core/src/payloads/delete_messages.rs +++ b/crates/teloxide-core/src/payloads/delete_messages.rs @@ -14,6 +14,7 @@ impl_payload! { /// Identifiers of 1-100 messages to delete. See [`DeleteMessage`] for limitations on which messages can be deleted /// /// [`DeleteMessage`]: crate::payloads::DeleteMessage + #[serde(with = "crate::types::vec_msg_id_as_vec_int")] pub message_ids: Vec [collect], } } diff --git a/crates/teloxide-core/src/payloads/forward_messages.rs b/crates/teloxide-core/src/payloads/forward_messages.rs index e191725b..e0ff6a23 100644 --- a/crates/teloxide-core/src/payloads/forward_messages.rs +++ b/crates/teloxide-core/src/payloads/forward_messages.rs @@ -16,6 +16,7 @@ impl_payload! { /// Unique identifier for the chat where the original message was sent (or channel username in the format `@channelusername`) pub from_chat_id: Recipient [into], /// A JSON-serialized list of 1-100 identifiers of messages in the chat _from\_chat\_id_ to forward. The identifiers must be specified in a strictly increasing order. + #[serde(with = "crate::types::vec_msg_id_as_vec_int")] pub message_ids: Vec [collect], } optional { diff --git a/crates/teloxide-core/src/types.rs b/crates/teloxide-core/src/types.rs index 3d10253b..465869d6 100644 --- a/crates/teloxide-core/src/types.rs +++ b/crates/teloxide-core/src/types.rs @@ -546,3 +546,35 @@ pub(crate) mod option_msg_id_as_int { } } } + +pub(crate) mod vec_msg_id_as_vec_int { + use crate::types::MessageId; + + use serde::{ser::SerializeSeq, Serializer}; + + pub(crate) fn serialize(msg_ids: &Vec, serializer: S) -> Result + where + S: Serializer, + { + let mut seq = serializer.serialize_seq(Some(msg_ids.len()))?; + for e in msg_ids { + seq.serialize_element(&e.0)?; + } + seq.end() + } + + #[test] + fn test() { + #[derive(serde::Serialize)] + struct Struct { + #[serde(with = "crate::types::vec_msg_id_as_vec_int")] + msg_ids: Vec, + } + + { + let s = Struct {msg_ids: vec![MessageId(1), MessageId(2)]}; + let json = serde_json::to_string(&s).unwrap(); + assert_eq!(json, "{\"msg_ids\":[1,2]}"); + } + } +} From 83f5508495dd27ba25ed21d7b667c7a0d6035bd0 Mon Sep 17 00:00:00 2001 From: LasterAlex Date: Fri, 30 Aug 2024 09:15:28 +0300 Subject: [PATCH 2/3] Cargo fmt --- crates/teloxide-core/src/payloads/codegen.rs | 6 ++---- crates/teloxide-core/src/types.rs | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/crates/teloxide-core/src/payloads/codegen.rs b/crates/teloxide-core/src/payloads/codegen.rs index 03bb9957..f4660b2f 100644 --- a/crates/teloxide-core/src/payloads/codegen.rs +++ b/crates/teloxide-core/src/payloads/codegen.rs @@ -223,10 +223,8 @@ fn params(params: impl Iterator>) -> String { { "\n #[serde(flatten)]" } - Type::ArrayOf(b) if **b == Type::RawTy("MessageId".to_string()) => - { - "\n #[serde(with = \ - \"crate::types::vec_msg_id_as_vec_int\")]" + Type::ArrayOf(b) if **b == Type::RawTy("MessageId".to_string()) => { + "\n #[serde(with = \"crate::types::vec_msg_id_as_vec_int\")]" } _ => "", }; diff --git a/crates/teloxide-core/src/types.rs b/crates/teloxide-core/src/types.rs index 465869d6..44308524 100644 --- a/crates/teloxide-core/src/types.rs +++ b/crates/teloxide-core/src/types.rs @@ -572,7 +572,7 @@ pub(crate) mod vec_msg_id_as_vec_int { } { - let s = Struct {msg_ids: vec![MessageId(1), MessageId(2)]}; + let s = Struct { msg_ids: vec![MessageId(1), MessageId(2)] }; let json = serde_json::to_string(&s).unwrap(); assert_eq!(json, "{\"msg_ids\":[1,2]}"); } From 173fe6e90d7753781fb3a96ae25aa5a09783bfcb Mon Sep 17 00:00:00 2001 From: LasterAlex Date: Fri, 30 Aug 2024 09:21:26 +0300 Subject: [PATCH 3/3] CHANGELOG added fix --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac09cb91..e9a314e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Some dependencies was bumped: `sqlx` to `0.8.1`, `tower` to `0.5.0`, `reqwest` to `0.12.7` - `tokio` version was explicitly specified as `1.39` +### Fixed + +- Now Vec in requests serializes into [number] instead of [ {message_id: number} ], `forward_messages`, `copy_messages` and `delete_messages` now work properly + [pr1147]: https://github.com/teloxide/teloxide/pull/1147 ## 0.13.0 - 2024-08-16