diff --git a/Cargo.lock b/Cargo.lock index 0a1963b2..2efead8b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2230,7 +2230,7 @@ dependencies = [ "serde_cbor", "serde_json", "sqlx", - "teloxide-core 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", + "teloxide-core", "teloxide-macros 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "thiserror", "tokio", @@ -2277,37 +2277,6 @@ dependencies = [ "xshell", ] -[[package]] -name = "teloxide-core" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4308e2880a535d8c30e494d548af1deb573e1fc06f2574fdd01b8fccf7c801a" -dependencies = [ - "bitflags 1.3.2", - "bytes", - "chrono", - "derive_more", - "either", - "futures", - "log", - "mime", - "once_cell", - "pin-project", - "rc-box", - "reqwest", - "serde", - "serde_json", - "serde_with", - "take_mut", - "takecell", - "thiserror", - "tokio", - "tokio-util", - "url", - "uuid", - "vecrem", -] - [[package]] name = "teloxide-macros" version = "0.8.0" diff --git a/crates/teloxide-core/src/types.rs b/crates/teloxide-core/src/types.rs index 3d222acf..407394da 100644 --- a/crates/teloxide-core/src/types.rs +++ b/crates/teloxide-core/src/types.rs @@ -447,6 +447,45 @@ pub(crate) mod option_url_from_string { } } +// Issue https://github.com/teloxide/teloxide/issues/1135 +// Workaround to avoid flattening with serde-multipart requests (involving +// file-manipulations) +pub(crate) mod msg_id_as_int { + use crate::types::MessageId; + + use serde::{Deserialize, Deserializer, Serialize, Serializer}; + + pub(crate) fn serialize(MessageId(id): &MessageId, serializer: S) -> Result + where + S: Serializer, + { + id.serialize(serializer) + } + + pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + i32::deserialize(deserializer).map(MessageId) + } + + #[test] + fn test() { + #[derive(Serialize, Deserialize)] + struct Struct { + #[serde(with = "crate::types::msg_id_as_int")] + message_id: MessageId, + } + + { + let json = r#"{"message_id":123}"#; + let s: Struct = serde_json::from_str(json).unwrap(); + assert_eq!(s.message_id, MessageId(123)); + assert_eq!(serde_json::to_string(&s).unwrap(), json.to_owned()); + } + } +} + pub(crate) mod option_msg_id_as_int { use crate::types::MessageId; diff --git a/crates/teloxide-core/src/types/message_id.rs b/crates/teloxide-core/src/types/message_id.rs index 8d2e573a..186f431d 100644 --- a/crates/teloxide-core/src/types/message_id.rs +++ b/crates/teloxide-core/src/types/message_id.rs @@ -22,6 +22,9 @@ pub struct MessageId(pub i32); // // (we can't change the default format of `MessageId` because it's returned // by some methods and we can't change serialization there) +// If you flatten `MessageId` within serde-multipart request, it will fail, see https://github.com/teloxide/teloxide/issues/1135 +// Try to use `serde(with = "crate::types::msg_id_as_int")]` instead of +// `#[serde(flatten)]` #[derive(Serialize, Deserialize)] struct MessageIdRaw { diff --git a/crates/teloxide-core/src/types/reply_parameters.rs b/crates/teloxide-core/src/types/reply_parameters.rs index 09c1ede8..794c4896 100644 --- a/crates/teloxide-core/src/types/reply_parameters.rs +++ b/crates/teloxide-core/src/types/reply_parameters.rs @@ -8,7 +8,8 @@ use crate::types::{MessageEntity, MessageId, Recipient}; pub struct ReplyParameters { /// Identifier of the message that will be replied to in the current chat, /// or in the chat _chat\_id_ if it is specified - #[serde(flatten)] + // Issue https://github.com/teloxide/teloxide/issues/1135 + #[serde(with = "crate::types::msg_id_as_int")] pub message_id: MessageId, /// If the message to be replied to is from a different chat, unique /// identifier for the chat or username of the channel (in the format diff --git a/crates/teloxide/Cargo.toml b/crates/teloxide/Cargo.toml index c693fa82..30666195 100644 --- a/crates/teloxide/Cargo.toml +++ b/crates/teloxide/Cargo.toml @@ -77,7 +77,8 @@ full = [ [dependencies] -teloxide-core = { version = "0.10", default-features = false } +# replace me by the actual version when release, and return path when it's time to make 0-day fixes +teloxide-core = { path = "../../crates/teloxide-core", default-features = false } teloxide-macros = { version = "0.8", optional = true } serde_json = "1.0"