Merge pull request #1136 from syrtcevvi/fix/message_id_flatten_bug_multipart

Fix #1135
This commit is contained in:
Tima Kinsart 2024-08-17 11:15:04 +00:00 committed by GitHub
commit eb31e14428
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 53 additions and 34 deletions

33
Cargo.lock generated
View file

@ -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"

View file

@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## unreleased
### Fixed
- Issue, when using `ReplyParameters` and multipart-requests involving file-sending it failed with `unimplemented error` ([#1136][pr1136], issue [#1135][issue1135])
[pr1136]: https://github.com/teloxide/teloxide/pull/1136
[issue1135]: https://github.com/teloxide/teloxide/issues/1135
## 0.10.0 - 2024-08-16
### Added

View file

@ -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<S>(MessageId(id): &MessageId, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
id.serialize(serializer)
}
pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<MessageId, D::Error>
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;

View file

@ -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 {

View file

@ -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

View file

@ -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"