Merge pull request #137 from teloxide/fix_message_entity_serialization

Fix message entity serialization
This commit is contained in:
Hirrolot 2021-11-24 18:36:51 +06:00 committed by GitHub
commit 5c1774b69b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 14 deletions

View file

@ -43,9 +43,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Type of `Poll::open_period`: `i32` -> `u16` ([#119][pr119])
- `Throttle` adaptor not honouring chat/min limits ([#121][pr121])
- Make `SendPoll::poll_` optional ([#133][pr133])
- Bug with `caption_entities`, see issue [#473][issue473]
[pr119]: https://github.com/teloxide/teloxide-core/pull/119
[pr133]: https://github.com/teloxide/teloxide-core/pull/133
[issue473]: https://github.com/teloxide/teloxide/issues/473
## 0.3.3 - 2021-08-03

View file

@ -58,6 +58,7 @@ bitflags = { version = "1.2", optional = true }
[dev-dependencies]
pretty_env_logger = "0.4"
tokio = { version = "1.8.0", features = ["fs", "macros"] }
[features]
default = ["native-tls"]

View file

@ -29,3 +29,22 @@ pub(crate) fn to_form<T: ?Sized + Serialize>(val: &T) -> impl Future<Output = Re
let fut = val.serialize(MultipartTopLvlSerializer {});
async { Ok(fut?.await?) }
}
// https://github.com/teloxide/teloxide/issues/473
#[tokio::test]
async fn issue_473() {
use crate::{
payloads::{self, SendPhotoSetters},
types::{InputFile, MessageEntity, MessageEntityKind},
};
to_form(
&payloads::SendPhoto::new(0, InputFile::file_id("0")).caption_entities([MessageEntity {
kind: MessageEntityKind::Url,
offset: 0,
length: 0,
}]),
)
.await
.unwrap();
}

View file

@ -60,9 +60,10 @@ impl From<Error> for RequestError {
Error::Io(ioerr) => RequestError::Io(ioerr),
// this should be ok since we don't write request those may trigger errors and
// Error is internal.
_ => unreachable!(
e => unreachable!(
"we don't create requests those fail to serialize (if you see this, open an issue \
:|)"
:|): {}",
e
),
}
}
@ -609,19 +610,23 @@ impl SerializeSeq for InnerPartSerializer {
// NOTE: this is probably highly inefficient (especially for ::Memory),
// but at least it works
let mut value = serde_json::to_value(value)?;
let file: InputFile = serde_json::from_value(value["media"].take())?;
match file {
f @ InputFile::Memory { .. } | f @ InputFile::File(_) => {
let uuid = uuid::Uuid::new_v4().to_string();
value["media"] = serde_json::Value::String(format!("attach://{}", uuid));
self.files.push((uuid, f));
}
InputFile::FileId(s) => {
value["media"] = serde_json::Value::String(s);
}
InputFile::Url(s) => {
value["media"] = serde_json::Value::String(String::from(s));
// Update value if it contains InputFile in a `media` field (i.e.: `InputMedia`)
if let Some(file) = value.get_mut("media") {
let file: InputFile = serde_json::from_value(file.take())?;
match file {
f @ InputFile::Memory { .. } | f @ InputFile::File(_) => {
let uuid = uuid::Uuid::new_v4().to_string();
value["media"] = serde_json::Value::String(format!("attach://{}", uuid));
self.files.push((uuid, f));
}
InputFile::FileId(s) => {
value["media"] = serde_json::Value::String(s);
}
InputFile::Url(s) => {
value["media"] = serde_json::Value::String(String::from(s));
}
}
}